5

Below is my code to read items from a document library.

var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('DocumentList')/items?$select=customerID";
$.ajax({  
    url: url,  
    method: "GET",  
    headers: {  
        "Accept": "application/json; odata=verbose"  
    },  
    success: function(data) {  
        $.each(data.d.results, function(index, item) {
            arrayCustomerID[index] = item.customerID;
        });  
    },  
    error: function(data) {  
        console.log("error");  
    }  
});  

This work for the normal case but what should I do so that it works if I have more than 5000 items in the document library.

2

2 Answers 2

14

Please refer below code :

Here we are doing recursive call to the GetListItems() function. This will fetch data in the bunch of 1000 items and concat it to response variable.

 var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('DocumentList')/items?$select=customerID&$top=1000";
    var response = response || [];  // this variable is used for storing list items
    function GetListItems(){
        return $.ajax({
            url: url,  
            method: "GET",  
            headers: {  
                "Accept": "application/json; odata=verbose"  
            },
            success: function(data){
                response = response.concat(data.d.results);
                if (data.d.__next) {
                    url = data.d.__next;
                    GetListItems();
                }
                $.each(response, function(index, item) {
                    arrayCustomerID[index] = item.customerID;
                });
            },
            error: function(error){
            }
        });
    }
10
  • so if (data.d.__next) will enter loop only if item number is 1001?
    – KumarV
    Jun 7, 2017 at 12:27
  • 1
    @KumarV data.d.__next contains the url to get the next items. In case we don't have more items left to fetch, it will not call GetListItems() function. Jun 7, 2017 at 12:29
  • that mean I have to write my code inside this if loop,right?
    – KumarV
    Jun 7, 2017 at 12:38
  • 1
    @RohitWaghela it is working now. I made a mistake myself. So above code worked, thanks.
    – KevinvG
    Jul 31, 2017 at 6:32
  • 3
    If we apply some filter like _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('DocumentList')/items?$filter= customerID eq 'someValue'&$top=1000" then will it work?
    – Harsh
    Dec 13, 2017 at 8:49
1

THIS IS WORKING !!! SOLVED

If your list or library is having more than 5000 items, then you need to perform a repeated ajax call function filtering 5000 items each time. But to know how many times to call the ajax function, we must do in Dynamic way.

  1. First call a ajax function with parameters "/_api/Web/Lists/GetByTitle(ListName)/Items?$orderby=Id desc&$top=1". Now you will get the latest added "Id".
  2. Now Divide the Id returned by the ajax by 5000. (Id/5000) So that you will get a result of how many times you need to perform the ajax.
  3. Now you can perform the ajax function repeatedly be filtering every 5000 items, with filter like, $filter=Id ge 0 and Id le 5000, $filter=Id ge 5000 and Id le 10000, ........ , etc.
  4. You can have a foreach loop to perform the ajax repeatedly with a dynamic filter, $filter=Id ge (5000*LoopValue) and Id le (5000*(LoopValue+1)).

Also make sure, the ajax to have async:true, to avoid performance issue. You can store the returned data in array and perform further actions, so that you could store more than 5000 data to perform your functions.

Since, there is a threshold limit for there kinda issues, we could use this kind of alternatives to make it possible.

2
  • While this will work, please note that MAX(Id) != COUNT(list items) as soon as some list item is deleted, so this is a bit hacky.
    – Jussi Palo
    Jul 30, 2020 at 7:17
  • Wouldn't a better way be to start by getting Gt 0 and then finding the max id returned and then get the next batch with id greater than that?? That way, you call them until the results are empty; right? Jan 27, 2021 at 2:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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