Posted by: R Manimaran | October 15, 2014

SharePoint 2013 REST API: Search using HTTP POST

SharePoint 2013 REST API: Search using HTTP POST

SharePoint 2013 provides REST API to do Search. There are two flavors of REST API for Search.

  • One which uses the HTTP GET
  • Second which uses the HTTP POST

Below is the sample to call the Search using the HTTP GET

http://sp2013/_api/search/query?querytext=’SharePoint

But the Limitation in using the GET method with querytext is the length of the query string. Max Query text length allowed by the Keyword query is 4096 characters.

In order to overcome this limitations we can use the HTTP Post method to do search.

Resource End Point Url for POST Query: /_api/search/postquery

Below is the syntax of our ajax call to Post query

</pre>
///method: postquery resource: Microsoft.Office.Server.Search.REST.SearchService
$.ajax(
{
'url':'restSource',
'method':'POST',
'data':JSON.stringify({
'request':{
'__metadata':{
'type':'Microsoft.Office.Server.Search.REST.SearchRequest'
},
'BlockDedupeMode':null,
'BypassResultTypes':null,
'ClientType':null,
'CollapseSpecification':null,
'Culture':null,
'DesiredSnippetLength':null,
'EnableFQL':null,
'EnableInterleaving':null,
'EnableNicknames':null,
'EnableOrderingHitHighlightedProperty':null,
'EnablePhonetic':null,
'EnableQueryRules':null,
'EnableSorting':null,
'EnableStemming':null,
'GenerateBlockRankLog':null,
'HiddenConstraints':null,
'HitHighlightedMultivaluePropertyLimit':null,
'HitHighlightedProperties':{'results': ['']},
'ImpressionId':null,
'MaxSnippetLength':null,
'PersonalizationData':null,
'ProcessBestBets':null,
'ProcessPersonalFavorites':null,
'Properties':{'results':[{
       '__metadata':{
       'type':'Microsoft.Office.Server.Search.REST.QueryProperty'
           },
'Name':null,
'Value':{
      '__metadata':{
      'type':'Microsoft.SharePoint.Client.Search.Query.QueryPropertyValue'
},
'BoolVal':null,
'IntVal':null,
'QueryPropertyValueTypeIndex':null,
'StrArray':{'results': ['']},
'StrVal':null
}
}]},
'QueryTag':null,
'QueryTemplate':null,
'QueryTemplatePropertiesUrl':null,
'Querytext':null,
'RankingModelId':null,
'RefinementFilters':{'results': ['']},
'Refiners':null,
'ReorderingRules':{'results':[{
      '__metadata':{
      'type':'Microsoft.SharePoint.Client.Search.Query.ReorderingRule'
        },
'Boost':null,
'MatchType':null,
'MatchValue':null
}]},

'ResultsUrl':null,
'RowLimit':null,
'RowsPerPage':null,
'SelectProperties':{'results': ['']},
'SortList':{'results':[{
       '__metadata':{
       'type':'Microsoft.SharePoint.Client.Search.Query.Sort'
       },
'Direction':null,
'Property':null
}]},

'SourceId':null,
'StartRow':null,
'SummaryLength':null,
'Timeout':null,
'TotalRowsExactMinimum':null,
'TrimDuplicates':null,
'TrimDuplicatesIncludeId':null,
'UILanguage':null
}
}),
'headers':{
'accept':'application/json;odata=verbose',
'content-type':'application/json;odata=verbose',
'X-RequestDigest':$('#__REQUESTDIGEST').val()
},
'success':function (data) {
       var d = data;
      },
'error':function (err) {
alert(JSON.stringify(err));
}
});

In this example I have placed a textbox and button to do the search. In the button click I have called this REST API call to bring the results using the keyword type in the textbox.

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

var endPointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/search/postquery";
var searchQuery = {
'request': {
'__metadata': { 'type': 'Microsoft.Office.Server.Search.REST.SearchRequest' },
'Querytext': "'" + $('#txtSearchText').val() + "'",// getting the user feed information from a TextBox
}
};
var call = jQuery.ajax({
      url: endPointUrl,
      type: "POST",
      data: JSON.stringify(searchQuery),
      headers: headers,
      dataType: 'json'
    });

call.done(function (data, textStatus, jqXHR) {
var html=[];
html.push("<table><thead><tr><th>ID</th><th>Title</th></thead>");
var queryResults = data.d.postquery.PrimaryQueryResult.RelevantResults.Table.Rows.results;
   if (queryResults.length > 0) {
       for (var i = 0; i < queryResults.length; i++) {
           var r = queryResults[i];
           var cells = r.Cells;
           var title = '';
           var id = '';
            for (var x = 0; x < cells.results.length; x++) {
            var c = cells.results[x];
            switch(c.Key){
               case "LastModifiedTime": id = c.Value; break;
               case "Title": title = c.Value; break;
             }

if (id != '' || title != '') {
html.push("<tr>");
html.push("<td>");
html.push(id);
html.push("</td>");
html.push("<td>");
html.push(title);
html.push("</td>");
html.push("</tr>");
}
}
}
html.push("</table>");
$("#displayDiv").html(html.join(''));
}
else {
    alert("No Results Found");
}
});
call.fail(function (data) {
    alert("Failure" + JSON.stringify(data));
});
}

Responses

  1. […] https://rmanimaran.wordpress.com/2014/10/15/sharepoint-2013-rest-api-search-using-http-post/ […]

  2. am not able access more than 4000 query length using this postquery

  3. postquery not accepting more than 4096 characters, please suggest.

  4. Post Query character limitation(4000) can be elevated by referring your query in Refinement Filter instead referring it in Query Template or Query Text.


Leave a comment

Categories