It always has to be a POST request. The request header is the same as the other normal post requests for SharePoint Rest API.
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
'content-type': 'application/json;odata=verbose',
'accept': 'application/json;odata=verbose'
}
The REST endpoint for using CAML query is:
/_api/web/lists/GetByTitle('<List Name>')/GetItems
The complete code with the REST call is given below:
// Include JQuery reference
// ... script continues ...
/// List Name and CAML Query as Parameter
function restCallwithCaml(listName, caml) {
/// get the site url
var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
/// set request data
var data = { "query" : {"__metadata": { "type": "SP.CamlQuery" },
"ViewXml":caml}
};
/// make an ajax call
$.ajax({
url: siteUrl+"/_api/web/lists/GetByTitle
('"+ listName +"')/GetItems",
method: "POST",
data: data,
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
'content-type': 'application/json;odata=verbose',
'accept': 'application/json;odata=verbose'
}
success: function (response) {
///do your code
},
error: function (data) {
///do your code
}
});
}
The CAML Query has to use in between this <View><Query>…CAML Query…</Query></View> tag. The query with the way to call the function is given below:
var caml = "<View><Query><Where><Eq><FieldRef Name='EndDate' /><Value Type='DateTime'><Today /></Value></Eq></Where></View></Query>";
restCallwithCaml(<listName>, caml);
No comments:
Post a Comment