SharePoint Get list items by CAML query - REST API
Below is the javascript code for getting list items by CAML Query.
function executeJson(url, method, headers, payload) {
method = method || 'GET';
headers = headers || {};
headers["Accept"] = "application/json;odata=verbose";
if (method == "POST") {
headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
}
var ajaxOptions = {
url: url,
type: method,
contentType: "application/json;odata=verbose",
headers: headers
};
if (typeof payload != 'undefined') {
ajaxOptions.data = JSON.stringify(payload);
}
return $.ajax(ajaxOptions);
}
function getListItemsByCAML(webUrl, listTitle, queryText) {
var viewXml = '<View><Query>' + queryText + '</Query></View>';
var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems";
var queryPayload = {
'query': {
'__metadata': {
'type': 'SP.CamlQuery'
},
'ViewXml': viewXml
}
};
return executeJson(url, "POST", null, queryPayload);
}
//Construct the CAML query
var queryText = "<OrderBy><FieldRef Name=\"SortOrder\" /></OrderBy><Where></Where>";
getListItemsByCAML(_spPageContextInfo.webAbsoluteUrl, 'Master.LeftNavigation', queryText)
.done(function(data) {
var items = data.d.results;
storeWithExpiration.set('DPA_LeftNav', {
site: _spPageContextInfo.webAbsoluteUrl,
items: items
},
300000); //5 minutes
setLefNav(items);
})
.fail(
function(error) {
console.log(JSON.stringify(error));
});
Comments
Post a Comment