Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / SharePoint

SharePoint 2013 Search Results Paging

0.00/5 (No votes)
28 Sep 2015CPOL1 min read 9.7K  
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.

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

The post SharePoint 2013 Search Results Paging appeared first on SharePoint Guide.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)