CHUVASH.eu

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

10 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 в Стокгольме http://sharepoint-community.net/events/sharepoint-saturday-stockholm/showAttendees?status=attending Тогда там встретимся.

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

        Анатолий, к сожалению, в Стокгольм пока никак не получается, но возможно получится пересечься где-нибудь еще. Последние два года бывал на SharePoint Connect Amsterdam http://www.nccomms.com/sharepoint_connect/Home.aspx.

        Анатолий, по теме поста столкнулся еще с одним вопросом, возможно Вы знаете ответ. Можно ли как-то организовать пэйджинг данным способом (в смысле 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:

    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);
    }
    });

  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 !!

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Discovering SharePoint

And having fun doing it

Bram de Jager talking Office 365, SharePoint and Azure

My view and thoughts on Productivity and more.

My programming life

and everything in between

SharePoint Development Lab by @avishnyakov

It is a good place to share some SharePoint stories and development practices.

SharePoint Dragons

Nikander & Margriet on SharePoint

RealActivity - Real-time and trustworthy

Blog site of founder, RealActivty - Paul J. Swider

Mai Omar Desouki - Avid SharePointer

Egyptian & Vodafoner - Senior SharePoint Consultant

Cameron Dwyer | Office 365, SharePoint, Outlook, OnePlace Solutions

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

paul.tavares

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

SPJoel

SharePoint for everyone

SharePointRyan

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, ya mama

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint

blksthl

Mostly what I know about SharePoint - CommunicoCuspis

%d bloggers like this: