0

I need to search inside a particular SharePoint (SharePoint 2016 on premise) document library using postquery with Rest API.
For that using GET call, I found a way,

http://{site_url}/_api/search/query?querytext='{query_text}+path:"{document_library_url}"'


And its working correctly. But how can I implement this in POST call (using postquery).

Edit

Here is how I tried,

//Search button click...
function searchKBtnClick() {
    var searchKey = $("#txtSearch").val() + "+path:'{document_library_url}'";

    var searchQuery = {
        'request': {
            '__metadata': { 'type': 'Microsoft.Office.Server.Search.REST.SearchRequest' },
            'Querytext': "'" + searchKey + "'",
            'SelectProperties': {
                'results': ['DisplayAuthor', 'Title', 'Path', 'Url', 'ContentSource']
            },
            'TrimDuplicates': true,
        }
    };

    getItemsPostQuery("/_api/search/postquery", searchQuery).done(function (data) {
        var result = JSON.stringify(data.d.postquery.PrimaryQueryResult.RelevantResults.Table.Rows.results);
        var resultCount = data.d.postquery.PrimaryQueryResult.RelevantResults.RowCount;
        $("#txtResult").val(result);

    });

};

And post call,

//Get search result from sharepoint (POST)
function getItemsPostQuery(url, data) {
    return $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + url,
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "content-Type": "application/json;odata=verbose"
        },
        data: JSON.stringify(data)
    });
}

But getting no search result.

1

To use postquery, you need to hit the /_api/search/postquery endpoint.

Try and modify the below code as per your requirement. Ensure that jquery is available before running the code:

var headers = {
        "Accept": "application/json;odata=verbose",
        "content-Type": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
}

//change url to your site 
var endPointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/search/postquery";
var searchQuery = {
'request': {
    '__metadata': { 'type': 'Microsoft.Office.Server.Search.REST.SearchRequest' },
    //your query text, change values here
    'Querytext': "'{query_text}+path:"{document_library_url}"", 
    }
};
$.ajax({
      url: endPointUrl,
      type: "POST",
      data: JSON.stringify(searchQuery),
      headers: headers,
      dataType: 'json',
      success:function(data){
         //use this to bind data to html
         console.log(data.d.postquery.PrimaryQueryResult.RelevantResults.Table.Rows.results);        
      },
      error:function(data){
        console.log("something went wrong");
      }
});

Reference - SP Search Rest API overview

  • I already tried to do it, but it returns zero search results – Vishnu S Jun 28 '17 at 10:16
0

Found problem in my code, I should have passed the Querytext attribute in this way,

'Querytext': $("#txtSearch").val() + "& path:{document_library_url}"

instead of this,

'Querytext': $("#txtSearch").val() + "+path:'{document_library_url}'"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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