SharePoint Rest API Query more than 5000 items

SharePoint REST API by default will return only 100 items

Use “$top” to retrieve more than 100 items but up to 5000 only: (we cannot get more than 5000 items)

.webAbsoluteUrl + "/_api/web/lists/getbytitle('myList')/items?$top=1000

Use Recursive call to retrieve more than 5000 items:

    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.

we will concatenate all json results into a variable & we can use the variable.
 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
            }
        });
    }

Filters not work when we have more than 5000 items in Rest API:

    We can use 2 approaches, 
    1.Either we can use the above method to concatenate json results into one variable & filter from json
    2.Or we can use indexing the column. We need add the column(which one you want to filter based on the column) as indexed column and filter based on it.  (List Settings > Indexed Columns > Create a new Index > select column to index)

Refer this Links for more details Link1, Link2

Comments

Popular posts from this blog

Add content type to SharePoint List/Library using REST API

Upload Single/Multiple file by using the REST API and jQuery SharePoint 2013

A type named 'SP.Data. could not be resolved by the model error