When we try to access more than 5000 items at a time, which is the default threshold limit, we get an error. This is true even we try to access items using the OOB list view web part. In this article, we will see how we can overcome this situation while accessing more than 5000 records using the REST API.
List view web part threshold message
When you access a SharePoint list which is exceeding the threshold limit of the list set by the administrator, you will get a similar error message like below. In most cases, the default limit is 5000.
Read more than 5000 rows using SharePoint REST API
- On your page you can call the function GetAllThresholdItems. This will get all the records there in your list.
- This code gets the data in the chunk of 1000 rows and stores it into a variable.
- After getting the first 1000 records, it checks data.d.__next value. If the next set of records is available, data.d.__next will have the url value in it which can be used again to call the same function. If it doesn’t have the value, mean you are at the end of the records.
-
function GetAllThresholdItems() {
-
var url = _spPageContextInfo.webAbsoluteUrl +
"/_api/web/lists/getbytitle('list
name')/items?$select=Id&$top=1000";
-
-
// Variable to store the list item values.
-
var results = results || [];
-
GetRows();
-
function GetRows() {
-
return $.ajax({
-
url: url,
-
method: "GET",
-
headers: {
-
"Accept":
"application/json; odata=verbose"
-
},
-
success: function (data) {
-
results = results.concat(data.d.results);
-
// If there's next set of data,
"data.d.__next" will contain URL. If not then you’re at the end of the records list and the execution of the code will stop.
-
if (data.d.__next) {
-
url = data.d.__next;
-
GetRows();
-
}
-
},
-
error: function (error) {
-
}
-
});
-
}
}
Conclusion:
In this article, we saw how to write an effective REST API script to overcome the threshold issues while programming in SharePoint.