For getting SharePoint list values through REST. I came across two solutions:
- writing CAML Query in the body of AJAX call
- Odata query selector operators (eg: select, expand) in the url
Are there any significant differences to watch out for?
A few benefits of REST ODATA queries:
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.
$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?
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.