Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
67 views
in SharePoint Server by 23 38 44
recategorized by

I am using a Sharepoint REST API that retrive data from a list, but it doesn't bring all items of the list.

Below is the SharePoint API used to get all list items but it's just return the top 100 items!

/_api/web/lists/GetByTitle('List')/Items/

How to retrive all list items using the SharePoint REST API?


1 Answer

0 like 0 dislike
by 164 225 450

How to retrive all list items using the SharePoint REST API?

By default, the SharePoint REST API returns only the top 100 items in a single query due to its default paging mechanism.

To retrieve all items from the list, you need to handle paging using the odata.nextLink property or specify a higher limit using the $top query parameter.

Here's how you can retrieve all items:

1) Use $top to Increase the Number of Items

You can specify the $top parameter to request a higher number of items in one query, up to a maximum of 5000 (the threshold limit for SharePoint):

/_api/web/lists/GetByTitle('List')/Items?$top=5000

If your list contains more than 5000 items, you'll still need to handle paging.

2) Handle Pagination with odata.nextLink

The SharePoint REST API includes an odata.nextLink property in the response when there are additional items to fetch.
You need to make multiple requests iteratively until all items are retrieved as below:

async function getAllItems(listTitle) {
    let items = [];
    let url = `/_api/web/lists/GetByTitle('${listTitle}')/Items?$top=5000`;

    while (url) {
        const response = await fetch(url, {
            headers: { 'Accept': 'application/json;odata=nometadata' },
        });
        const data = await response.json();
        items = items.concat(data.value);
        url = data['odata.nextLink']; // Get the next page link
    }

    return items;
}

// Usage
getAllItems('List').then((items) => console.log(items));

Important Notes

You have to consider the below key notes when consuming the SharePoint REST API:

  • If your list exceeds 5000 items, you'll need to use paging or indexed columns for filtering to stay within SharePoint's list view threshold.
  • Minimize data transfer by retrieving only the necessary fields using the $select parameter.

      /_api/web/lists/GetByTitle('List')/Items?$select=Title,ID
    
  • Ensure the user or app accessing the API has appropriate permissions to read all items.

See Also, SharePoint .NET Server, CSOM, JSOM, and REST API index

If you don’t ask, the answer is always NO!
...