Programming DASL Search

WebDAV Ajax Library provides methods for detecting DASL search support, submitting search queries, and requesting custom properties to be returned in search results. You can specify which properties to search and if file content should be searched.

Probing Search Support

The WebDAV server that supports DASL search returns SEARCH token in the Allow header. The WebDAV Ajax Library provides HierarchyItem.GetSupportedFeaturesAsync() function that returns information about features supported by the server. This function returns OptionsInfo.Features flags enumeration containing all features supported by the server. The GetSupportedFeaturesAsync() call submits OPTIONS request to the server and if the search is supported the Features.Dasl flag is set.

var webDavSession = new ITHit.WebDAV.Client.WebDavSession();

webDavSession.OpenFolderAsync('https://serv/', null, function(oAsyncResult){
    /** @typedef {ITHit.WebDAV.Client.Folder} oFolder */
    var oFolder = oAsyncResult.Result;

    oFolder.GetSupportedFeaturesAsync(function(oAsyncResult){
        /** @typedef {ITHit.WebDAV.Client.OptionsInfo} oOptionsInfo */
        var oOptionsInfo = oAsyncResult.Result;

        var bSearch = oOptionsInfo.Features & ITHit.WebDAV.Client.Features.Dasl !== 0;
        console.log('Search support: ' + (bSearch ? 'yes' : 'no'));
    });
});

Submitting Search Query

To submit a search request the library provides Folder.SearchByQueryAsync() function. Here is the example of search request:

var webDavSession = new ITHit.WebDAV.Client.WebDavSession();

webDavSession.OpenFolderAsync('https://serv/folder/', null, function(oFolderAsyncResult) {

    /** @typedef {ITHit.WebDAV.Client.Folder} oFolder */
    var oFolder = oFolderAsyncResult.Result;

    // Build search query
    var oSearchQuery = new ITHit.WebDAV.Client.SearchQuery('Sales Report%');

    // By default WebDAV Ajax Client search by DisplayName property.
    // You can add other properties to this list.
    oSearchQuery.LikeProperties.push(new ITHit.WebDAV.Client.PropertyName('creator-displayname', 'DAV:'));
    oSearchQuery.LikeProperties.push(new ITHit.WebDAV.Client.PropertyName('comment', 'DAV:'));

    // Disable search by file content
    oSearchQuery.EnableContains = false;

    oFolder.SearchByQueryAsync(oSearchQuery, function(oAsyncResult) {

        /** @typedef {ITHit.WebDAV.Client.HierarchyItems[]} aItems */
        var aItems = oAsyncResult.Result;

        for (var i = 0, l = aItems.length; i < l; i++) {
            console.log(aItems[i].DisplayName);
        }

    });
});

By default, the search will be performed using the display name and content. You can use SearchQuery.LikeProperties list to add more properties to the search list.

Note that the  Folder.SearchAsync() and  Folder.SearchByQueryAsync() functions may return a very long list of items. Instead of getting all items, you can request a specified range using Folder.GetSearchPageAsync() and Folder.GetSearchPageByQueryAsync() function. See Paging Through Children Items and Search Results article for more details.

Requesting Custom Properties

In many cases, you will need additional data being returned with each hierarchy item in search results. For example, you may need a piece of text around the search phrase or name of the author that updated the document last time. You can request additional properties by specifying them in a search request in SearchQuery.SelectProperties list. The requested properties will be returned in HierarchyItem.Properties:

var oSearchQuery = new ITHit.WebDAV.Client.SearchQuery('Sales Report%');

// Ask server to return MyNS:snippet property with each item.
var snippetPropName = new ITHit.WebDAV.Client.PropertyName('snippet', 'MyNS:')
oSearchQuery.SearchProperties = [snippetPropName];

oFolder.SearchByQueryAsync(oSearchQuery, function(oAsyncResult) {

/** @typedef {ITHit.WebDAV.Client.HierarchyItems[]} aItems */
var aItems = oAsyncResult.Result;

for (var i = 0, l = aItems.length; i < l; i++) {
    console.log(aItems[i].DisplayName + ' ' +
        aItems[i].Properties.Find(snippetPropName).StringValue());
    }
});

Search Phrase Wildcards and Escaping

To add wildcards to your search phrase use DASL wildcard characters:

  • ‘%’ – to indicate one or more characters.
  • ‘_’ – to indicate exactly one character.
new ITHit.WebDAV.Client.SearchQuery('Sales Report 201_');

Note that the WebDAV Ajax Library will pass the search phrase to the server without modification. As though if ‘%’, ‘_’ or ‘\’ characters are used in a search phrase, they must be escaped as ‘\%’, ‘\_’ and ‘\\’.

See Also:

Next Article:

Managing Custom Properties with JavaScript WebDAV Library