none
Pagination for results in search REST Api with jQuery RRS feed

  • Question

  • Good afternoon, what would be the best approach to implement pagination for the results from REST API call? I have been trying to make the call using rowlimit, startrow and rowsperpage properties in the query sent to the API but I see no change. I'm using just jQuery and REST API calls to accomplish this.

    This is the query sent to the API service:

    var searchURL = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?queryText='" + query + "'&rowlimit=500&startrow=0&RowsPerPage=10&enableSorting=true";

    As you can see at the bottom of the image, I would like to implement the links to navigate through the pages based on the items per page value selected in the dropdown list which I guess would be the RowsPerPage property value, is that correct?

    Any help would be appreciated, thanks in advance.



    Carlos M


    • Edited by Carlos MC Wednesday, September 12, 2018 10:15 PM Info
    Wednesday, September 12, 2018 9:26 PM

All replies

  • Hi,

    Use startrow and rowlimit in search REST API. Learn more about SharePoint Search REST API.

    For example:

    function search(queryText,rowLimit,startRow,allResults)
    
    {
    
        var allResults = allResults || [];
    
        var url = _spPageContextInfo.siteAbsoluteUrl + "/_api/search/query?querytext='" + queryText + "'&rowlimit=" + rowLimit + "'&startrow=" + startRow + "&sortlist='LastModifiedTime:ascending";
    
        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(queryText,rowLimit,startRow+relevantResults.RowCount,allResults);
    
               }   
    
               return allResults;
    
        });
    
    }


    Call the above search function when clicking the button.

    For example:

    search(_spPageContextInfo.webAbsoluteUrl,'sharepoint',100,0)
    
      .done(function(results){
    
           for(var i = 0;i < results.length;i++){
    
               //do something…
    
           }
    
       });


    Demo about how get search results with pagination:

    https://sharepoint.stackexchange.com/questions/208462/sharepoint-search-js-returns-only-500-search-results/208468

    Best regards,

    Linda Zhang


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Thursday, September 13, 2018 9:13 AM
    Moderator
  • For some reason, the $.getJSON is not working with sharepoint I tried with different service and I got results but not for sharepoint. I made the request on this using different approach

                                                           

    function getSearchResultsREST(_query, _region, _rowLimit, _startRow) {

    _query = encodeURIComponent(_query);

    var searchURL = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?queryText='" + _query + "'&rowlimit=" + _rowLimit + "&startrow=" + _startRow + "&enableSorting=true";

    $.ajax({
    url: searchURL,
    method: "GET",
    headers: {
    "Accept": "application/json; odata=verbose"
    },
    contentType: "application/json; odata=verbose",
    success: function(data) {
    SearchResultsOnSuccess(data, _region, _startRow);
    },
    error: function(error) {
    $('#related-content-results').html(JSON.stringify(error));
    }
    });
    }

    function SearchResultsOnSuccess(data, _region, _startRow, allResults) {
    var allResults = allResults || [];
    var results;
    var divHTML = '';

    if (data.d) {
    var relevantResults = data.d.query.PrimaryQueryResult.RelevantResults;
    allresults = allResults.concat(relevantResults.Table.Rows);

    results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
    if(results.length == 0) {
    $('#related-content-results').html("There is No data for the requested query on " + _spPageContextInfo.webAbsoluteUrl);
    } else {
    for (i=0; i<results.length; i++) {
    var item = results[i];
    var itemCell = item.Cells;
    var itemResults = itemCell.results;

    // Get values for item result
    var _title = getValueByKey("Title", itemResults);
    var _path = getValueByKey("Path", itemResults);

    divHTML += "<li style='list-style:none'><div class='cbs-Item'><a href=" + _path + ">" + _title + "</a></div></li>";
    }

    if(relevantResults.TotalRows > _startRow+relevantResults.RowCount) {

    // Pagination.
    $('#noPaginationDR').hide();
    $('#paginationLA').show();

    divHTMLPages = "";

            // Check complete pages
            var pagesInCurrent = Math.floor(relevantResults.TotalRows / relevantResults.RowCount);

            // Check remainder of pages
            var recordsRemainder = relevantResults.TotalRows % relevantResults.RowCount;

    if (recordsRemainder != 0) { pagesInCurrent += 1; }
                                    // Displaying number of pages according to complete and remainder 
            for (i=1; i<=pagesInCurrent; i++) {
    divHTMLPages += "<div>" + i + "</div>";
            }

            // Show page numbers.
            $('#pageNumber').html(divHTMLPages);

            // Decrement in one to match the full pages in case used for pages.
            if (recordsRemainder != 0) { pagesInCurrent--; }
            if (recordsRemainder == 0) {
    var startItemPositionLastPage = relevantResults.TotalRows - relevantResults.RowCount + 1;
            } else {
            var startItemPositionLastPage = (pagesInCurrent * relevantResults.RowCount) + 1;
    }
    } else if (relevantResults.TotalRows <= relevantResults.RowCount){ 

                               $('#noPagination').show();
    }

    }
    }
    }

    Specifically in this code:

            for (i=1; i<=pagesInCurrent; i++) {
             divHTMLPages += "<div>" + i + "</div>";
            }

    I display the number of pages at the bottom (stored in i variable), is this good approach to try to concatenate html code to perform the call to the getSearchResultsREST function? I would concatenate with the query, rowLimit and startRow or should I change it? Thanks in advance for your advice.



    Carlos M

    Friday, September 14, 2018 12:52 AM
  • Hi,

    Yes, you can.

    It is a good approach.

    Best Regards,

    Linda Zhang


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, September 17, 2018 2:37 AM
    Moderator
  • Hi,

    You can mark your own solution as the answer.

    Best regards, 

    Linda Zhang


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, October 3, 2018 8:58 AM
    Moderator