Click here to Skip to main content
15,885,366 members
Articles / Productivity Apps and Services / Sharepoint
Tip/Trick

SharePoint REST API: CAML Query

Rate me:
Please Sign up or sign in to vote.
4.43/5 (5 votes)
12 Jun 2017CPOL 28.7K   6   4
This is a tip about using CAML query more efficiently with SharePoint Rest API.

Introduction

Last week, I was working with SharePoint REST API. In some terms, I need to do some complicated query in a SP list. To do these queries, I prefer CAML. But to use CAML with SharePoint REST API, I have been banging my head against the wall for a couple of days as I was stacked with errors. At last, I got the solution that I wanted.

Using the Code

So what I missed is the first condition of using CAML with REST request. That is, it always has to be a POST request. The request header is the same as the other normal post requests for SharePoint Rest API.

JavaScript
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:

JavaScript
// 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:

JavaScript
var caml = "<View><Query><Where><Eq>
<FieldRef Name='EndDate' /><Value Type='DateTime'>
<Today /></Value></Eq></Where></View></Query>";
restCallwithCaml(<listName>, caml);

Points of Interest

The CAML query can also be used in query string of the REST request. But it makes the URL unnecessarily complicated.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Genweb2
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIs there an error in the code Pin
jeff9748-Jun-17 18:06
jeff9748-Jun-17 18:06 
AnswerRe: Is there an error in the code Pin
Md. Tahmidul Abedin12-Jun-17 20:29
professionalMd. Tahmidul Abedin12-Jun-17 20:29 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun12-Apr-17 2:15
Humayun Kabir Mamun12-Apr-17 2:15 
GeneralRe: My vote of 5 Pin
Md. Tahmidul Abedin16-Apr-17 19:53
professionalMd. Tahmidul Abedin16-Apr-17 19:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.