0

For getting SharePoint list values through REST. I came across two solutions:

  1. writing CAML Query in the body of AJAX call
  2. Odata query selector operators (eg: select, expand) in the url

Are there any significant differences to watch out for?

2 Answers 2

3

A few benefits of REST ODATA queries:

  • Much easier to write.
  • Generally more readable and obvious to another developer.
  • Might be a tiny bit faster, but probably not enough to notice. (The ODATA query and the CAML query are both processed on the server.)
  • Easy to create and test as an HTTP GET.

A few benefits of CAML queries:

  • You can create more complex queries.

  • You can query on properties that may not be exposed by ODATA.

  • The query does not have to be part of the URL, therefore is more secure, especially with HTTPS.

  • The query does not have to be part of the URL, therefore has no length limitations. (Even when part of the query string, it must be sent as an HTTP POST as you need to include the "X-RequestDigest".)

  • Can be intermixed with ODATA commands ($select, etc) when used GetItems.

  • Supports CAML recursive.

    Example of a more complex REST query that uses both CAML and ODATA to get all folders and subfolders in a library:

    /sites/yourSite/_api/web/lists/getbytitle('Documents')/GetItems(query=@v1)?@v1={'ViewXml':'<View Scope=\'RecursiveAll\'><Query><Where><Eq><FieldRef Name=\'FSObjType\' ></FieldRef><Value Type=\'LookUp\'>1</Value></Eq></Where></Query></View>'}&$select=FileDirRef,fileleafref
    
    Method: POST
    
    Header: {"accept": "application/json;odata=verbose",
                       "content-type": "application/json;odata=verbose",
                       "content-length":__len__,
                       "X-RequestDigest": "__yourRequestDigest__"}
    

I generally use CAML queries only when I can't use ODATA queries.

The above all assumes we are talking about the SharePoint REST API.

2
  • glad for your reply. I got some knowledge on those topics. Commented Sep 24, 2018 at 6:08
  • It seems you can't use $expand with CAML though... so you have to re-query to get the linked fields. Or is there some workaround I'm missing for this? Commented Aug 4 at 17:19
1

Based on my experience, I think Odata query selectors are better in performance. As it gets only those particular data.

However, in case of Caml query, it gets the data based on the rest url and then filters based on the query which loses the performance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.