2

How to use and understood CAML query condition and how to apply the condition to SharePoint's REST API? Please see my CAML query condition and my REST API code also.

CAML query condition:

"<View>
  <Query>
    <Where>
      <And>
        <And>
          <Neq>
            <FieldRef Name='Author'  LookupId='True' />
            <Value Type='User'>111</Value>
          </Neq>
          <Neq>
            <FieldRef Name='PIC'  LookupId='True' />
            <Value Type='UserMulti'>111</Value>
          </Neq>
        </And>
        <And>
          <Neq>
            <FieldRef Name='MasterPIC'  LookupId='True'  />
            <Value Type='UserMulti'>111</Value>
          </Neq>
          <Neq>
            <FieldRef Name='GatekeepingPic'  LookupId='True' />
            <Value Type='UserMulti'>111</Value>
          </Neq>
        </And>
      </And>
    </Where>
  </Query>
</View>"

REST API condition:

http://.../_api/lists/getbytitle('Library')/items?$select=TitleNew,Author/Id,Author/Title,MeetingDate,NgoName,Staff,MPicmailid,MasterPIC/Id,MasterPIC/Title,Editor/Id,Editor/Title&$expand=Author,MasterPIC,Editor&$filter=(Author/Id ne '111')and (MasterPIC/Id ne '111') and (Editor/Id ne '111')

How to understand the camel query condition? Please, anyone, Is both conditions are true? if it false in rest API please update my rest API condition? I need condition only to my rest API.

3 Answers 3

1
var Query = "[Your CAML Query]";
var endpointUrl = "/_api/web/lists/getbytitle('[ListName]')/getitems"
function getData() {
    var retval = '';    
    var datatoload = {
        'query': {
            '__metadata': {
                'type': 'SP.CamlQuery'
            },
            'ViewXml': Query
        }
    };

    $.ajax({        
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        data: JSON.stringify(datatoload),
        url: endpointUrl,
        success: function(data) {            
            for(var i=0;i<=data.d.results.length;i++)
            {
                console.log(data.d.results[i]);
            }
        },
        error: function(data) {
            alert(data);
            failure(data); // Do something with the error
        }
    });
}
2

Your rest API $filter must be changed to match the CAML query.

http://...//_api/Web/Lists/GetByTitle('Library')/items?$select=TitleNew,Author/Id,Author/Title,MeetingDate,NgoName,Staff,MPicmailid,MasterPIC/Id,MasterPIC/Title,Editor/Id,Editor/Title&$expand=Author,MasterPIC,Editor&$filter=(Author/Id ne '111') and (PIC/Id ne '111') and (MasterPIC/Id ne '111') and (GatekeepingPic/Id ne '111')

*Note:*In SharePoint rest API,
$select - which fields to return in results.
$expand - used to retrieve information from Lookup columns.
$filter - specifies which items to return.
For Reference check this link

2
  • Please can you change my rest api as match camel query condition. Commented Jul 11, 2017 at 10:07
  • @Hasanshali check updated answer Commented Jul 11, 2017 at 10:29
1

You can use your CAML query in SharePoint REST API too, then I don't think that you will need any overload to convert such big CAML to REST API Condition syntax.

Here is the working example:

var viewXml = "YOUR CAML QUERY";
var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('List Name')/getitems";
var queryPayload = {
    'query': {
        '__metadata': { 'type': 'SP.CamlQuery' },
        'ViewXml': viewXml
    }
};
return $.ajax({
    url: url,
    method: "POST",
    contentType: "application/json;odata=verbose",
    data: JSON.stringify(queryPayload),
    async: false,
    headers: {
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "Accept": "application/json; odata=verbose"
    },
    success: function (data) {
        var items = data.d.results;
    },
    failure: function (data) {
        console.log(data.responseText);
    }
});

Let me know if you need more clarifications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.