Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to do paging on a document library (also trying this on a list) with SP 2013 using REST/OData but I can't seem to get the skipping of items working.

Here are some examples of URLs that I'm trying on my own SP 2013 farm and it also doesn't work on my Office 365 2013 Dev Preview farm. Results are returned and $top and $orderby work fine but it never skips any items. i.e. I can take the $skip out of the URL and I get the same result.

http://sp15/_api/web/lists/GetByTitle('FAQ')/Items?$skip=2&$top=2&$orderby=ID
http://sp15/_api/web/lists/GetByTitle('FAQ')/Items?$skip=2
http://sp15/_api/web/lists/GetByTitle('FAQ')/Items?$skip=2&$top=2

http://sp15/_api/web/lists/GetByTitle('Documents')/Items?$skip=2&$top=2&$orderby=ID
http://sp15/_api/web/lists/GetByTitle('Documents')/Items?$skip=2&$top=2&$orderby=ID desc

etc.

Any ideas on what I'm doing wrong?

share|improve this question

6 Answers 6

up vote 0 down vote accepted

I'm not that familiar with REST but try the old syntax: Example: *http://sp15/_vti_bin/ListData.svc/FAQ?$skip=4&$top=2*

share|improve this answer

I see the same issue on SP2013 online with the new OData V3 _api endpoint. For CRUD operation it might be better to use the OData v2 listdata.svc endpoint for the time being.

share|improve this answer
1  
Paging sounds like something that will be added to the _api endpoint at somepoint soon - I'd go with this solution but make sure you pattern it in such a way so that you can swap out with code that DOES run on _api when that capability is added, it'll keep your code current and fully supported/upgradeable for vNext. –  James Love Nov 17 '12 at 22:15

The $skip parameter does not work in SharePoint 2013 for list items. It only works for collection of data (like list collections, ecc..). See my comment at the end of this article: http://msdn.microsoft.com/library/d4b5c277-ed50-420c-8a9b-860342284b72.aspx

share|improve this answer

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.

But you could utilize $skiptoken query option to return paged results as demonstrated below.

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 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);
}
share|improve this answer

Vadim's response is very close, but his query string is off a bit. The issue isn't really his, its Microsoft's as they are not being consistent with their URI escaping.

I used the Postman extension for Chrome and by examining the __next value returned when using $top (which is alternative approach worth considering) I noticed that there is an ampersand that is not escaped properly in the __next values. Using that approach I was able to get pagination with 2013 REST OData calls working. Here is the relevant code to use in conjunction with Vadim's response.

var queryString = "?%24skiptoken=Paged%3DTRUE%26p_ID%3D" + (startItemId - 1) + '&%24top=' + itemCount;
share|improve this answer
    
Does this still work if you apply an "orderby" some other field than the ID? –  SteveCl Feb 18 at 19:43
    
The p_ID value is paged ID, not the list item's ID (I think). The list I was testing on was sorted by modified date and things seemed to be coming back as expected but I must confess to not looking closely. –  El Hombre Feb 18 at 20:59

In my experience, none of the above solutions work - this could be because I was attempting this using an external list. To implement paging, I added ?$skiptoken=Paged=TRUE&$top=100 to the end of the URI. The URI for the next page will be included under JSON.d.__next. Once the last page is reached __next will not be present

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.