SPOnline Search REST API POST request
There is one thing about Sharepoint. It doesn't let you pass even if there is a slight formatting error in your request. And, most of the time error is not so appropriate or no error.
I faced the same situation when i tried to compose a POST request for SPOnline Search REST API. The query text was quite complex as I was trying to retrieve search results based on a managed property which is in turn is generated from managed metadata column. The key here is not introduce + symbol in place of space as we would generally do in GET request.
Code Snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Note: This is an angular implementation | |
function getRequestDigest() { | |
var deferred = $q.defer(); | |
var config = { | |
'DataServiceVersion': '3.0', | |
'Accept': 'application/json; odata=nometadata', | |
'Content-Type': 'application/json;odata=verbose' | |
} | |
var data = {}; | |
$http.post("../../_api/contextinfo", data, config).success(function (data, status, headers, config) { | |
deferred.resolve(data.FormDigestValue); | |
}).error(function (data, status, headers, config) { | |
deferred.reject(status); | |
}); | |
return deferred.promise; | |
} | |
//Actual function - a complex condition on managed property (which is a collection of managed metadata column) | |
getRequestDigest().then(function (resp) { | |
var config = { | |
headers: { | |
//'Authorization': 'Bearer ' + tokenId, //+(retryCount==0?'*':''), | |
'Accept': 'application/json;odata=verbose;charset=utf-8', | |
'Content-Type': 'application/json;odata=verbose;charset=utf-8', | |
'X-RequestDigest': resp | |
} | |
}; | |
var dataObject = {}; | |
dataObject.request = {}; | |
dataObject.request.Querytext = "path:\"https://mytenant.sharepoint.com/sites/mysitecollection\" (IsDocument:\"True\" OR contentclass:\"STS_ListItem\") AND AND (myManagedProperty:#06252568a8b-eee3-4055-be254-8ffd8c63042d OR myManagedProperty:#038853c8b-daf8-4326-bafe-4ce40a84b5c4) AND ((myManagedProperty:#0e2524f2576-8084-4a07-85bd-2255732ceda8d AND myManagedProperty:#0ed0f4605-88256-46d5-bed7-cbd0f4740c56) OR (myManagedProperty:#0a4f8bbac-2533b-4843-a845-8fabe28b425cd AND myManagedProperty:#06c7c35a25-a574-4385-80325-d6463f8c2875)) AND (myManagedProperty:#0ccd48ad7-025a5-4604-8d25b-3f868c2325558) -ContentClass=urn:content-class:SPSPeople"; | |
dataObject.request.RowLimit=500; | |
dataObject.request.SelectProperties={}; | |
dataObject.request.SelectProperties.results=['Title','LinkingUrl','ReferenceURL','Path','OriginalPath','FileType']; | |
dataObject.request.ClientType = 'ContentSearchRegular'; | |
data = JSON.stringify(dataObject); | |
console.log(dataObject); | |
console.log(data); | |
$http.post("../../_api/search/postquery", data, config).success(function (data, status, headers, config) { | |
deferred.resolve(data.d.postquery.PrimaryQueryResult.RelevantResults.Table.Rows.results); | |
}).error(function (data, status, headers, config) { | |
deferred.reject(status); | |
}); | |
}); |
Comments
Post a Comment
Feedback - positive or negative is welcome.