CHunky Universe of Vigourous Astonishing SHarepoint :)

Using CAML with SharePoint REST API

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:

  1. Inability to filter items based on multivalued taxonomy fields
  2. 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.
Needed HTTP Headers in REST requests

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" :
      { "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" :
              { "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);

14 responses to “Using CAML with SharePoint REST API

  1. baseic 2014-03-26 at 08:12

    Nicely written! I too just learned that CAML was an option in REST as well!

    • Anatoly Mironov 2014-03-26 at 08:22

      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.

  2. Mikhail Pushin 2015-01-19 at 10:53

    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.

    • Anatoly Mironov 2015-01-19 at 11:12

      спасибо за отзыв Михаил! Я видел что вы собираетесь поехать на SharePoint Saturday в Стокгольме Тогда там встретимся.

      • Mikhail Pushin 2015-01-29 at 15:58

        Анатолий, к сожалению, в Стокгольм пока никак не получается, но возможно получится пересечься где-нибудь еще. Последние два года бывал на SharePoint Connect Amsterdam

        Анатолий, по теме поста столкнулся еще с одним вопросом, возможно Вы знаете ответ. Можно ли как-то организовать пэйджинг данным способом (в смысле CAML+Rest)? Т.е. как-то получить и впихнуть ListItemCollectionPosition в запрос? Пробовал, игрался всяко-разно, но пока никак. Есть какие-нибудь идеи?

  3. William 2015-06-16 at 15:10

    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?

  4. Ali 2015-07-13 at 16:51

    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:

    headers: {
    success: function (data) {
    error: function (data) {

    • Anatoly Mironov 2015-07-13 at 21:38

      Cool! I am glad that it worked out. Thanks a lot for sharing!

      • Patrick Beauperin 2017-04-11 at 17:48

        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.

    • Chris 2018-01-19 at 23:29

      Excellent, JSON.stringify() fixed my issue, thanks a lot.
      @Anatoly – Great article, helped a lot.

  5. jjrnumba1 2015-07-21 at 23:37

    Thanks for posting this, very useful!

  6. Balaji 2016-06-28 at 14:38

    Thanks for sharing was helpful !!

  7. Nayan 2018-07-24 at 07:59

    Hi Anatoly, I am getting an empty result set for any of the library I query. Please help.
    “d”: {
    “results”: []

  8. Kiran 2019-01-25 at 06:19

    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?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Daniel Chronlund Cloud Tech Blog

News, tips and thoughts for Microsoft cloud fans

Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager - Architect, Speaker, Author

Microsoft 365, SharePoint and Azure

SharePoint Dragons

Nikander & Margriet on SharePoint

Mai Omar Desouki

PFE @ Microsoft

Cameron Dwyer

Office 365, SharePoint, Azure, OnePlace Solutions & Life's Other Little Wonders


Me and My doings!

Share SharePoint Points !

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

Aryan Nava

DevOps, Cloud and Blockchain Consultant


SharePoint for everyone


Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint


Mostly what I know and share about...

%d bloggers like this: