Written by

Jayakumar Balasubramaniam

Share

  

Category

see all categories
intranet as a service

Handling the SharePoint REST API search threshold limit in SPFx

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

Since we are searching across the tenant/site there is more chance you will end up with more than 500 rows per search.

Get the total number of rows expected to return through the search API call?

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?

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 more than 500 rows. This approach will fit for 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 text sharepoint,

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 start row parameter specifies from which row it has to start the search, for example if you specify the start row as 501, search will be performed from the 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 and make the second call to same search api with same refiners and parameters,

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

Code Snippet:

let totalRows: number = searchResult.PrimaryQueryResult.RelevantResults.TotalRows;

let pageSize = 500; //maximum rows that search api can return

if (totalRows > pageSize) {

     let totalPages = parseInt((totalRows / pageSize).toString());

        for (let page = 1; page <= totalPages; page++) {

                    let startRow = page * pageSize;

                    this.searchWithPaging(startRow);

            }

}

How to build SharePoint Framework Applications?

The following blog posts will help you for sure in detail about building SPFx applications,

                   Build SPFx webparts with Angular 4

                   Deep Dive into SPFx webparts configuration

                   Build SPFx extensions

                   Integrating SPFx apps with visual Studio

 

If you have any questions/issues about this article, please let me know in comments.

Jayakumar Balasubramaniam

Written by Jayakumar Balasubramaniam

A technology evangelist working on Microsoft 365, SharePoint & Azure. Gets my hands dirty with latest and greatest of SharePoint, Azure & front-end frameworks. Currently working with “Hubfly – A unified digital workplace” as a Product Engineer. Getting in to the depth of the domain & technology, with my eyes for perfection, ensures that the product releases go right every time.