Paging Through Children Items and Search Results

The functionality described in this article is available in IT Hit WebDAV Ajax Library v5.8 Beta and later versions.

WebDAV Ajax Library provides methods for requesting a specified range of items when listing folder content and searching. You can also specify specify sort conditions when listing folder content.

Probing Paging Support

The WebDAV server that supports paging feature returns paging token in the DAV header. The WebDAV Ajax Library provides HierarchyItem.GetSupportedFeaturesAsync() function that returns information about the 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 paging is supported the Features.Paging flag is set.

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

webDavSession.OpenFolderAsync('https://serv/folder/', 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 bPaging = oOptionsInfo.Features & ITHit.WebDAV.Client.Features.Paging !== 0;
        console.log('Paging support: ' + (bPaging ? 'yes' : 'no'));
    });
});

Paging Through Children Items

To request a specific range of items in a folder, the library provides  Folder.GetPageAsync() function. You can specify a number of items to skip before returning the rest of the items (offset), number of items to return (pageSize) and a list of sort conditions to be applied on a server side (sortColumns):

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

var offset = 10;                                                    // Number of items to skip.
var pageSize = 5;                                                   // Page size.
var sortColumns = [new ITHit.WebDAV.Client.OrderProperty(
    new ITHit.WebDAV.Client.PropertyName('name', 'myNs'), true)];   // Sort columns.

webDavSession.OpenFolderAsync('http://serv/folder/', null, function (oAsyncResult) {

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

    oFolder.GetPageAsync(null, offset, pageSize, sortColumns, function (oPagingAsyncResult) {

        /** @type {ITHit.WebDAV.Client.HierarchyItem[]} aItems */
        var aItems = oPagingAsyncResult.Result.Page;                                 // Items on the requested page.
        /** @type {number} totalPages */
        var totalPages = Math.ceil(oPagingAsyncResult.Result.TotalItems / pageSize); // Total number of pages.
    });
});

The callback returns the object that contains PageResults class, containing an array of items in Page property and a total number of items in this folder in TotalItems property. You can use TotalItems property to calculate the total number of pages available and render client user interface for pages navigation. 

Note that total number of pages is optional and can be null. In some systems the total number of pages can be resource-consuming to determine or is just not required.

Paging Through Search Results

To page through search results the library provides Folder.GetSearchPageAsync() and Folder.GetSearchPageByQueryAsync() functions. The parameters and return value are similar to the Folder.GetPageAsync() function:

var webDavSession = new ITHit.WebDAV.Client.WebDavSession();
var offset = 10;
var pageSize = 5;

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('my report%');
    oSearchQuery.EnableContains = false;   // Disable search by file content.

    oFolder.GetSearchPageByQueryAsync(oSearchQuery, offset, pageSize, function (oPagingAsyncResult) {

        /** @type {ITHit.WebDAV.Client.HierarchyItem[]} aItems */
        var aItems = oPagingAsyncResult.Result.Page;                                 // Items on the requested page.
        /** @type {number} totalPages */
        var totalPages = Math.ceil(oPagingAsyncResult.Result.TotalItems / pageSize); // Total number of pages.
    });
});

Note that unlike the Folder.GetPageAsync() function, the above functions do not provide sorting parameter. It is expected that the server-side indexing and search mechanism will return results that best match the query at the top of the list. 

See Also:

Next Article:

Programming DASL Search