SharePoint REST - get more than 100 list items

Since SharePoint 2013 you can use REST calls to get or set data. But if you firing a call against a list with 101 items you will get only 100. The cause is based at the REST interface. To reduce server overload the result is limited - it is called server paging. That is pretty smart, but what if you need all results?
There are two ways of solving this problem. The first and easy one is to set the $top flag. So you add ?$top=101 to your REST call and you will get all 101 items. This works fine until you have 102 items.

And this is how we come to way two. This one is a bit more coding, but it is dynamic. The key is to check the response for response.d.__next. This property is set, if there are some more items, you can get. So take it and fire a call with it and you will get the next 100 items.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var allResults = [];

$(document).ready(function(){
  var url = SharePointUrl + "/_api/web/lists/getbytitle('listName')/items";
  loadItems(url);
});

function loadItems(queryUrl) {
  $.ajax({  
    type: "GET",
    dataType: "json",
    url: queryUrl,
    success: function (response) {  
      // merdge results
      allResults = allResults.concat(response.d.results);  
      // if response.d.__next there are more items
      if (response.d.__next) {
        // repeat the call
        var newQueryUrl = response.d.__next;
        loadMore(newQueryUrl);
      }
      else {
        // done, start work with allResults
      }
    }, 
    error: function () { 
      alert("fail"); 
    }
  }); 
}

Hint: the limit of 100 items is only set to lists. If you are trying to call web/webs you will retrieve all items at once.

Comments

Popular posts from this blog

How to support multiple languages in WPF