Thursday, March 18, 2021

How to read more than 5000 items from document library in SharePoint RestApi?

  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.

 

 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){

            }

        });

    }

No comments:

Post a Comment