Egor
Egor

Reputation: 680

Sharepoint pagination

I'm trying create client-side pagination using Sharepoint Rest Api, i use $skip and $top parameters:

http://siteurl/_api/lists/getByTitle(list name)/items?$skip=0&$top=2

But with:

http://siteurl/_api/lists/getByTitle(list name)/items?$skip=2&$top=2

I have the same result as a first link

I know about __next url in response, but i need back and forwerd pagination between pages

Does anyone have usecases for solving this problem?

Upvotes: 1

Views: 8813

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

Currently $skip query option is not supported in SharePoint 2013/Online.

According to Use OData query operations in SharePoint REST requests:

The $skip query option does not work with queries for SharePoint list items.

There are several options for implementing client-side pagination using SharePoint REST Interface.

Option 1

Utilize $skiptoken query option to return paged results

Format: $skiptoken=Paged=TRUE&p_ID=<last item id to skip>&$top=<items count>

Example

The example demonstrates how to retrieve the limited count of items (2 items) with id equals or greater to 2 from Pages library:

function getPagedItems(webUrl,listTitle,startItemId,itemsCount)
{
    var endpointUrl = webUrl + "/_api/web/lists/getbytitle('" + listTitle +  "')/items?$skiptoken=" + encodeURIComponent('Paged=TRUE&p_SortBehavior=0&p_ID=' + (startItemId-1) + '&$top=' + itemsCount);
    return executeRequest(endpointUrl,'GET');
}


getPagedItems('https://contoso.sharepoint.com/','Pages',2,2)
.done(function(data){
    if(data.d.results.length == 0){
        console.log('Items not found');
        return;
    }
    for(var i = 0; i < data.d.results.length; i++){
        var item = data.d.results[i];
        console.log(item.Title);
    }   
});

where

function executeRequest(url,method,headers,payload) 
{
    if (typeof headers == 'undefined'){
        headers = {};
    }
    headers["Accept"] = "application/json;odata=verbose";
    if(method == "POST") {
        headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
    }   

    var ajaxOptions = 
    {       
       url: url,   
       type: method,  
       contentType: "application/json;odata=verbose",
       headers: headers
    };
    if(method == "POST") {
      ajaxOptions.data = JSON.stringify(payload);
    }  

    return $.ajax(ajaxOptions);
}

Option 2

Utilize SharePoint 2010 REST interface to retrieve paged results using $skip query option:

Endpoint example: https://contoso.sharepoint.com/_vti_bin/ListData.svc/Pages?$skip=2&$top=2

Upvotes: 4

Related Questions