Handling The SharePoint REST API Search Threshold Limit In SPFx

­­­­SharePoint REST APIs restrict results and return a limited amount of data to improve performance. One of the restrictions is the threshold limit to read data from a list which means we cannot read more than 5000 items from a list for an API call. The same rule applies to Search APIs also; it returns only 500 rows at a time.

Since we are searching across the tenant/site, there are more chances for us ending up with more than 500 rows per search.

How to get the total number of rows expected to be returned through the Search API call?

The total number of rows expected to be returned from your search query will be available in the below object mapping of the response.

PrimaryQueryResult.RelevantResults.TotalRows

Get the total number of rows returned in the current search request?

The total number of rows returned in the current search will be available in the below object mapping of the response.

PrimaryQueryResult.RelevantResults.Rowcount

The traditional pagination is the core concept to handle the search results in more than 500 rows. This approach will fit any type of technologies you use to contact SharePoint Search through Rest API with some slight changes in the headers. Let me explain this for SPFx now.

Check out our article about reading more than 5000 items using rest API in SharePoint Framework applications. Now, handling the threshold limit in search API is not so complicated like handling threshold limit for list items, just add the below parameter in the ODATA query,

startrow=501

Let me explain with an example. Below is a sample url to get the contents from the tenant with the Sharepoint text,

https://tenantName.sharepoint.com/search/_api/search/query?querytext='sharepoint’

We can also restrict the number of results to be returned from the Search API result with the parameter rowlimit like below,

https://tenantName.sharepoint.com/search/_api/search/query?querytext='sharepoint&rowlimit=500’

The startrow parameter specifies from which row it has to start the search, for example, if you specify the start row as 501, the search will be performed from row 501 and it will return the results from row 501 to row 1000.

Get the total rows expected to return from the search and put in the below logic. Then, make the second call to the same Search API with the same refiners and parameters.

URL

https://tenantName.sharepoint.com/search/_api/search/query?querytext='sharepoint&rowlimit=500&startrow=501’

Code Snippet
  1. let totalRows: number = searchResult.PrimaryQueryResult.RelevantResults.TotalRows;  
  2. let pageSize = 500; //maximum rows that search api can return  
  3. if (totalRows > pageSize) {  
  4.     let totalPages = parseInt((totalRows / pageSize).toString());  
  5.     for (let page = 1; page <= totalPages; page++) {  
  6.         let startRow = page * pageSize;  
  7.         this.searchWithPaging(startRow);  
  8.     }  
  9. }  
How to build SharePoint Framework Applications

The following blog posts will help you in detail about building SPFx applications,
If you have any questions/issues about this article, please let me know in the comments.