Wednesday, May 13, 2020

How to fetch more than 5000 items from lists?

Ans:- The solution is to use Recursive call to retrieve more than 5000 items.
    var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ListName')/items?$top=1000";
    var response = response || [];  // this variable is used for storing list items
    function GetListItems(){
        $.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();
                }
            },
            error: function(error){
                   // error handler code goes here
            }
        });
    }

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

Here data.d.__next contains the url to get the next set of items. In case we don’t have more items left to fetch, it will not call GetListItems() function.

No comments:

Post a Comment