Last active
November 26, 2016 16:40
-
-
Save pradeepkumargali/16cea12f48c98c1eaf8c163bac3fbf70 to your computer and use it in GitHub Desktop.
SharePoint Search REST POST function
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); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment