12

I have an AJAX call querying the SharePoint REST API which will return more than 500 rows:

$.ajax(
        {
             url: _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='Department:"
                    + "*'&selectproperties='Department'&sourceid='B09A7990-05EA-4AF9-81EF-EDFAB16C4E31'&sortlist='Department:ascending'&rowlimit=500",
                method: "GET",
                headers: {
                    "accept": "application/xml",
                },
                success: onSuccess,
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            }
        );

The REST API has a 500 row limit, so I cannot increase the rowlimit variable in the url.

In my onSuccess function, I'm trying to read the total rows to see if I need to query again for more rows:

onSuccess = function (data) {
    var rows = data.d.postquery.PrimaryQueryResult.RelevantResults.TotalRows;
....

This throws an exception that postquery does not exist, however.

How can I grab the TotalRows and query the REST API for additional rows?

4 Answers 4

6

See if these paths to the row count variables work:

data.d.query.PrimaryQueryResult.RelevantResults.RowCount

data.d.query.PrimaryQueryResult.RelevantResults.TotalRows

If you have specific questions about the object model, I find that the best first point of reference is to look at the returned xml or json using web developer tools and looking at the returned object graph.

7
  • Both of these throw an error saying query is not defined. If I change the response type to application/json, both of those DO work... RowCount gives me 500, the total rows in the current response, not the total rows for the query, TotalRows does give me that though (again, with the json response, not the xml). I don't want the response in json though.
    – tnw
    Feb 18, 2014 at 18:03
  • @FalakMahmood Not sure what you mean. That resides under the query object and that's not defined under the XML response. TotalRows already gives me what I want, but only with a json response.
    – tnw
    Feb 18, 2014 at 18:10
  • 1
    I don't have access to a 2013 farm for testing at the moment, but if you use the web developer tools in IE or Chrome, you can see the response and get an idea of the object model.
    – tyshock
    Feb 18, 2014 at 18:21
  • @tyshock Brilliant! I totally forgot about that, I can see the TotalRows property hiding in the XML. If you'd like to add that to your answer, I will accept it.
    – tnw
    Feb 18, 2014 at 18:38
  • 2
    For re-querying starting from the 500th row, you can use the startrow parameter. Eg: /_api/search/query?querytext='*'&startrow=500' Feb 18, 2014 at 19:00
12

Here is an example that demonstrates how to query all search results using SharePoint 2013 Search REST API:

function search(webUrl,queryText,rowLimit,startRow,allResults)
{
    var allResults = allResults || [];
    var url = webUrl + "/_api/search/query?querytext='" + queryText + "'&rowlimit=" + rowLimit + "'&startrow=" + startRow;
    return $.getJSON(url).then(function(data) {
           var relevantResults = data.PrimaryQueryResult.RelevantResults;
           allResults = allResults.concat(relevantResults.Table.Rows);
           if (relevantResults.TotalRows > startRow + relevantResults.RowCount) {
               return search(webUrl,queryText,rowLimit,startRow+relevantResults.RowCount,allResults);
           }    
           return allResults;
    });
}

Key points:

  • StartRow query parameter is used to specify next paged search results

Usage

search(_spPageContextInfo.webAbsoluteUrl,'sharepoint',100,0)
  .done(function(results){
       for(var i = 0;i < results.length;i++){
           console.log(results[i].Cells[3].Value);
       }
   });
1
4

You can raise the MaxRowLimit property of your Search service application (Only with On-Prem SharePoint installation) using PowerShell to overcome this limitation. But remember, it will affect the perform though.

3

Try these.

data.d.query.PrimaryQueryResult.RelevantResults.TotalRows

data.d.query.PrimaryQueryResult.RelevantResults.TotalRowsIncludingDuplicates

I tested on O365.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.