Using CAML with SharePoint REST API
By Anatoly Mironov
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.
[caption id=“3142” align=“alignnone” width=“536”] HTTP headers you have to provide in REST requests with CAML queries[/caption] You can use jQuery or SP.RequestExecutor to make an ajax call. The REST endpoint is: [sourcecode]_api/web/Lists/GetByTitle(’’)/GetItems[/sourcecode] The request body (if you use json, and I bet, you do) is in this format: [sourcecode language=“js”] { “query” : {"__metadata": { “type”: “SP.CamlQuery” } , “ViewXml”: “” } } [/sourcecode] Here is the boilerplate for a REST request with a CAML Query: [code language=“js”] 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” } }); } [/code] 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 [code language=“js”] var caml = “ ”; getDataWithCaml(“Tasks”, caml); [/code]
Comments from Wordpress.com
Anatoly Mironov - Jul 1, 2015
Cool! I am glad that it worked out. Thanks a lot for sharing!
Thanks for posting this, very useful!
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?
Анатолий, к сожалению, в Стокгольм пока никак не получается, но возможно получится пересечься где-нибудь еще. Последние два года бывал на SharePoint Connect Amsterdam http://www.nccomms.com/sharepoint_connect/Home.aspx. Анатолий, по теме поста столкнулся еще с одним вопросом, возможно Вы знаете ответ. Можно ли как-то организовать пэйджинг данным способом (в смысле CAML+Rest)? Т.е. как-то получить и впихнуть ListItemCollectionPosition в запрос? Пробовал, игрался всяко-разно, но пока никак. Есть какие-нибудь идеи?
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 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); } });
спасибо за отзыв Михаил! Я видел что вы собираетесь поехать на SharePoint Saturday в Стокгольме http://sharepoint-community.net/events/sharepoint-saturday-stockholm/showAttendees?status=attending Тогда там встретимся.
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.
Thanks for sharing was helpful !!
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,
Hi Anatoly, I am getting an empty result set for any of the library I query. Please help. { “d”: { “results”: [] } }
Excellent, JSON.stringify() fixed my issue, thanks a lot. @Anatoly - Great article, helped a lot.
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?