SharePoint 2013 Search Results Paging

When we do a search query using KQL, by default SharePoint limits the results to 50 rows. We can explicitly set the RowLimit upto 500.

If our result set contains more items, we have to page through the results. This can be done by keeping the current row index value(which is the last row of the current result set), looping through the pages and updating the current row index after fetching the current page.

KeywordQuery keywordQuery = new KeywordQuery(siteCollection);
SearchExecutor searchExecutor = new SearchExecutor();
keywordQuery.QueryText = "KQL Query String";
keywordQuery.StartRow = 0;
keywordQuery.RowLimit = 50;

//retrieve the first page of results
ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery);
var resultTables = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults);
var resultTable = resultTables.FirstOrDefault();
DataTable resultDataTable = resultTable.Table;

int currentRowIndex = 0;
//Iterate through the rest of the pages
while (resultTable.TotalRowsIncludingDuplicates > resultDataTable.Rows.Count)
{
    //Update the current row index
    currentRowIndex += resultDataTable.Rows.Count;
    resultTableCollection = GetSearchResults(keywordQuery, currentRowIndex, searchExecutor);
    var searchResults = resultTableCollection.FirstOrDefault();
    if (searchResults.RowCount <= 0)
        break;
    else
        resultDataTable.Merge(searchResults.Table);
}

In the above code, we retrieve the first 50 results. If the total results is greater than current set of results, we access the remaining results in batches of 50. We process each batch  which in this case is to append to the datatable. So, after we process all the batches, the resultDataTable will have the complete results even if it is a large result set.

Building the KQL Query

The QueryText needs to be set to a valid KQL Query String. This can consist of free-text including wildcards like sharep*.
We can provide property restrictions such as author:”Prashanth Padebettu” note that there should be no space around the : like ” : “. The : is the contains operator, so we can search if a property contains a specific text for example. If the value you are searching contains spaces, enclose it in double quotes like shown in the example above.

There are other operators such as < (less than), >(greater than), <>(not equal to) etc. Apart from Text, it supports various data types such as Integer, Double, Decimal, DateTime() etc. KQL also supports operators such as AND, NOT and OR to build complex queries.

Check out the MSDN KQL reference article Keyword Query Language (KQL) syntax reference

One thought on “SharePoint 2013 Search Results Paging”

  1. Thank you for posting, this code is very useful. Though this includes a logic error. The line currentRowIndex += resultDataTable.Rows.Count; incorrectly increments on subsequent iterations of the loop. I recommend removing and updating the following line to resultTableCollection = GetSearchResults(keywordQuery, resultDataTable.Rows.Count, searchExecutor); for desired results.

Leave a Reply

Your email address will not be published. Required fields are marked *