Do you prefer REST over CSOM as I do? I’ll skip the whys. Andrew Connell put it already in wrtiting so nicely. Well, if you do prefer REST, then you must have discovered some shortcomings of REST, or its incompleteness compared to CSOM. I think of:
- Inability to filter items based on multivalued taxonomy fields
- Inability to filter items based on user fields where user is added through a group, rather than directly, e.g. AssignedTo=[Me] combined with a SharePoint group.
- …
In such situations I was forced to use CSOM. Until yesterday. Yesterday I learned that we can actually use CAML queries in REST requests.
This enables using REST in all situations. The REST API is still developed and many features are added. Maybe a particular operation that needs a CAML query today, can be supported in the core REST API and can be easily refactored then.
But until then, we can use CAML queries in REST requests. Here are the important things about it:
- A REST request with a CAML query is always a POST request
- A REST request with a CAML query has always to have X-RequestDigest http header (actually because it is a POST request)
- A REST request with a CAML query should always have the attached CAML query in the request body (and not in a query string). We don’t want to mess with long urls, do we?
- A REST request with a CAML query must have the http header “Content-Type: application/json;odata=verbose” unless you use xml in the request body.

HTTP headers you have to provide in REST requests with CAML queries
You can use jQuery or SP.RequestExecutor to make an ajax call. The REST endpoint is:
_api/web/Lists/GetByTitle('<your list title>')/GetItems
The request body (if you use json, and I bet, you do) is in this format:
{ "query" :
{"__metadata":
{ "type": "SP.CamlQuery" }
, "ViewXml": "<YOUR CAML QUERY>"
}
}
Here is the boilerplate for a REST request with a CAML Query:
function getDataWithCaml(listName, caml) {
var endpoint = "/_api/web/lists/GetByTitle('"
+ listName + "')/GetItems";
var requestData = { "query" :
{"__metadata":
{ "type": "SP.CamlQuery" }
, "ViewXml": caml
}
};
return jQuery.ajax({
url: endpoint,
method: "POST",
data: requestData,
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"Accept": "application/json; odata=verbose",
"Content-Type": "application/json; odata=verbose"
}
});
}
This function is just an example. It has no error handling, and it takes for granted that your list is in the root site for on your (sub-)domain (“/”). So take it as an example only.
Here is how the function can be invoked
var caml = "<View><Query><Where><Or><Eq><FieldRef Name='AssignedTo' /><Value Type='Integer'><UserID/></Value></Eq><Membership Type='CurrentUserGroups'><FieldRef Name='AssignedTo' /> </Membership></Or></Where></Query></View>";
getDataWithCaml("Tasks", caml);
Like this:
Like Loading...
Nicely written! I too just learned that CAML was an option in REST as well!
Thank for your feedback. Appreciate it. It is so much in SharePoint. I am learning new things all the time. SharePointStack is a good place to gather knowledge.
Thanks for the great information, Anatoly. This is exactly what I needed. I stumbled into the limitation of REST and your post has helped me to find an easy solution.
спасибо за отзыв Михаил! Я видел что вы собираетесь поехать на SharePoint Saturday в Стокгольме http://sharepoint-community.net/events/sharepoint-saturday-stockholm/showAttendees?status=attending Тогда там встретимся.
Анатолий, к сожалению, в Стокгольм пока никак не получается, но возможно получится пересечься где-нибудь еще. Последние два года бывал на SharePoint Connect Amsterdam http://www.nccomms.com/sharepoint_connect/Home.aspx.
Анатолий, по теме поста столкнулся еще с одним вопросом, возможно Вы знаете ответ. Можно ли как-то организовать пэйджинг данным способом (в смысле CAML+Rest)? Т.е. как-то получить и впихнуть ListItemCollectionPosition в запрос? Пробовал, игрался всяко-разно, но пока никак. Есть какие-нибудь идеи?
I’m using this way to call “Pages” library. I found I have to make another call to server to get “FileRef” field value, directly calling CAML + REST API doesn’t return “FileRef” field value…it even doesn’t return file name…Do you have any luck?
Thanks for explaining this concept Anatoly, it’s very clear and well explained. I needed to make a REST call and use $filter on an “External Data” column; however, I would get an error that the field type of “BusinessData” could not be used in the query filter expression. So, instead, the method of using a CAML query helped me.
However, one thing that I was having trouble with using the CAML query w/ REST was this error:
{“error”:{“code”:”-1, Microsoft.SharePoint.Client.…-US”,”value”:”Not well formatted JSON stream.”}
I battled with this for a few hours, until my search brought me to a Stack Overflow answer that mentioned to used “JSON.stringify()” to format JavaScript strings/params to proper JSON format; so by wrapping the “requestData” in that function, I was able to successfully send the request to the server:
jQuery.ajax({
url:svcUrl,
method:”POST”,
data:JSON.stringify(requestData),
headers: {
“X-RequestDigest”:jQuery(“#__REQUESTDIGEST”).val(),
“Accept”:”application/json;odata=verbose”,
“Content-Type”:”application/json;odata=verbose”
},
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
Cool! I am glad that it worked out. Thanks a lot for sharing!
Hi Anatoly, thanks a lot for this helpful post. But Ali’s comment is really helpful and I strongly suggest you update your code wrt his suggestion. At least mention this case in your post.
I had exactly the same situation as Ali and JSON.stringify addition made all the difference.
Regards,
Excellent, JSON.stringify() fixed my issue, thanks a lot.
@Anatoly – Great article, helped a lot.
Thanks for posting this, very useful!
Thanks for sharing was helpful !!
Hi Anatoly, I am getting an empty result set for any of the library I query. Please help.
{
“d”: {
“results”: []
}
}
Thanks for sharing. But one thing that I was having problems to fetch lookup values using CAML with SharePoint REST API. How to retrieve lookup values?