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)); }); }
[…] https://rmanimaran.wordpress.com/2014/10/15/sharepoint-2013-rest-api-search-using-http-post/ […]
By: SharePoint 2013 REST API: Search using HTTP POST | Dinesh Ram Kali. on September 8, 2015
at 6:07 am
am not able access more than 4000 query length using this postquery
By: Durga on May 5, 2016
at 2:00 pm
what the browser you are using? here are the max length supported by the Browsers.
http://stackoverflow.com/questions/812925/what-is-the-maximum-possible-length-of-a-query-string
By: R Manimaran on May 14, 2016
at 12:32 am
postquery not accepting more than 4096 characters, please suggest.
By: Durga on May 13, 2016
at 12:45 pm
Post Query character limitation(4000) can be elevated by referring your query in Refinement Filter instead referring it in Query Template or Query Text.
By: Anbarasan Sadagopi on July 20, 2016
at 1:29 pm