none
How can I use CAML queries with the REST API?

    Question

  • This page shows how to retrieve list items for a specific CAML query, using the JavaScript Object Model:

    http://msdn.microsoft.com/en-us/library/hh185007.aspx

    How can I achieve the same result with the new REST (_api) interface?

    For example, if I have a CAML expression like this:
    var camlQuery=''<View><Query><Where><Geq><FieldRef Name=\'ID\'/><Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>''

    How can I send this CAML expression to the server via the REST interface and get the matching items?


    Christophe Humbert Path to SharePoint http://blog.PathToSharePoint.com User Managed Solutions http://UserManagedSolutions.com


    Monday, November 12, 2012 10:46 PM

Answers

  • I was able to get the following to work:

    POST https://<site>/_api/web/Lists/GetByTitle('[list name]')/GetItems(query=@v1)?@v1={"ViewXml":"<View><Query>[other CAML query elements]</Query></View>"}

    Hope it helps you!

    Erik

    Thursday, September 19, 2013 7:22 PM

All replies

  • Wednesday, November 14, 2012 7:03 AM
    Moderator
  • Hi GuYuming,

    You are posting the link from my question, how is this supposed to help?

    Thanks,

    Christophe


    Christophe Humbert Path to SharePoint http://blog.PathToSharePoint.com User Managed Solutions http://UserManagedSolutions.com

    Wednesday, November 14, 2012 4:23 PM
  • Thursday, November 15, 2012 6:26 AM
    Moderator
  • ok :-)

    Maybe my initial question was not clear, so I have edited it to add an example. I have CAML expressions, and I am looking for a way to retrieve the list items that match these CAML expressions via the REST API.

    Your link shows how to use the REST API with OData queries, but what I am after is a way to use the REST API with CAML queries.


    Christophe Humbert Path to SharePoint http://blog.PathToSharePoint.com User Managed Solutions http://UserManagedSolutions.com

    Thursday, November 15, 2012 7:24 AM
  • As far as I know, you can't pass CAML into rest request. But you don't need to pass CAML inot REST as REST itself provides querying features that covers almost all CAML querying functionalities. You can find more details on how you can generate REST URL which represents your CAML : http://ranaictiu-technicalblog.blogspot.com/2012/12/sharepoint-2013-use-caml-into-rest.html

    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com

    Sunday, December 02, 2012 1:39 PM
  • thanks for the link Sohel.

    I am asking about CAML because this is the format that gets passed to my application. Converting it to a REST query, as explained in your article, makes sense, but then it would have to be done automatically within the code, not manually.

    If there was a way to directly get the REST query from a SharePoint list view, this would be even better, but as far as I know it's not possible. This is why I am retrieving CAML.


    Christophe Humbert Path to SharePoint http://blog.PathToSharePoint.com User Managed Solutions http://UserManagedSolutions.com

    Sunday, December 02, 2012 7:01 PM
  • You can't use CAML with the REST services in SharePoint.

    If you have to use the CAML supplied to you I'd suggest that you look at using the client query api

    Tuesday, December 11, 2012 4:56 AM
  • Right, that's actually what I was saying in my question. Unfortunately the client Object Model is much heavier than REST, and that's what I am trying to move away from.

    Christophe Humbert Path to SharePoint http://blog.PathToSharePoint.com User Managed Solutions http://UserManagedSolutions.com

    Tuesday, December 11, 2012 5:06 PM
  • I am marking your reply as answer as this seems to be the best we can do with REST. The final answer seems to be that the REST api can retrieve CAML but not post it.

    Christophe Humbert Path to SharePoint http://blog.PathToSharePoint.com User Managed Solutions http://UserManagedSolutions.com

    Tuesday, December 11, 2012 5:10 PM
  • As far as I know, you can't pass CAML into rest request. But you don't need to pass CAML inot REST as REST itself provides querying features that covers almost all CAML querying functionalities. You can find more details on how you can generate REST URL which represents your CAML : http://ranaictiu-technicalblog.blogspot.com/2012/12/sharepoint-2013-use-caml-into-rest.html

    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com

    I love when people say "you don't need to"...

    Simple task: get items from a list view using REST.

    In this case, I can get the list view query in CAML but it is not usable by the REST filter API -

    Simple task? not really. Couldn't find a way to do it in REST since it doesn't support CAML query format.

    So, I guess I do need to - to the thread opener - did you ever got a solution for it?


    Regards, Shai Petel.

    Wednesday, August 07, 2013 8:50 PM
  • We are on the same page. The REST API allows you to retrieve CAML but then there's nothing you can do with it.

    I don't know why this was marked as answer and I have unmarked it. I think Gavin's answer is the correct one. If you need to use CAML then just stick to the legacy SOAP services... (well, that's what I did).

    Christophe


    Christophe Humbert Path to SharePoint http://blog.PathToSharePoint.com User Managed Solutions http://UserManagedSolutions.com

    Wednesday, August 07, 2013 9:17 PM
  • Well, it is somewhat the answer I guess.

    the simple answer is you can't and it is not supported.

    I just wish you could at least get the items from an existing view, but I guess back to CSOM


    Regards, Shai Petel.

    Thursday, August 08, 2013 12:35 AM
  • That's a different question. You might want to take a look at the RenderAsHtml method for views.

    Christophe Humbert Path to SharePoint http://blog.PathToSharePoint.com User Managed Solutions http://UserManagedSolutions.com

    Thursday, August 08, 2013 5:18 AM
  • Thanks, all I need is the list of items in the view (IDs only), I'm afraid getting it as HTML, parsing it and extracting item IDs would be a lot of work (dev and runtime).

    I rather use CSOM, I just will have to wait for the client context to be ready before I do it.


    Regards, Shai Petel.

    Thursday, August 08, 2013 12:57 PM
  • I was just correcting you and saying it's possible with REST, not that it's the best way...

    Christophe Humbert Path to SharePoint http://blog.PathToSharePoint.com User Managed Solutions http://UserManagedSolutions.com

    Thursday, August 08, 2013 4:24 PM
  • Yes, you are right, still we need CAML as there's no clue now how we can convert CAML to REST query format. But having an way of converting  CAML to REST query sometimes, by using JavaScript library (expecting an open source implementation from someone) or  by using SharePoint REST API from MS (like http://server/_api/web/list(guid'list-guid')/views(guid'view-guid)/restquery), maybe we'll get rid of CAML sometime for REST.

    Since REST protocol supports it's own query feature, I reckon MS will try to stick with the standard, since REST is the introduced as a way to exposing data to any client application (PHP, Java etc.) not just SharePoint or MS-based applications. So yes still we need CAML for now in REST, but maybe we'll not need it sometimes later as REST API becomes rich and we expect it to be sooner.


    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com>

    Friday, August 09, 2013 1:14 AM
  • I think we can all agree on one thing:

    If rest does not use CAML as filter, it should not return CAML from view queries. right?

    I totally agree, I have no special need to desire to work with CAML format, if only I could get it in another format it would be awesome.

    Well, it is a rather new API approach - it is awesome and very useful and fast (even within SharePoint - it is just so much faster).

    Hope someone would extend it, to at least allow you to get items by view. That would be awesome.


    Regards, Shai Petel.

    Friday, August 09, 2013 5:32 PM
  • I was able to get the following to work:

    POST https://<site>/_api/web/Lists/GetByTitle('[list name]')/GetItems(query=@v1)?@v1={"ViewXml":"<View><Query>[other CAML query elements]</Query></View>"}

    Hope it helps you!

    Erik

    Thursday, September 19, 2013 7:22 PM
  • If it works it would be awesome!

    I wish it was documented, where did you learn about this option?

    Can't wait to try it.


    Regards, Shai Petel.

    Thursday, September 19, 2013 8:46 PM
  • Erik, I am impressed!

    Thanks!

    Christophe


    Christophe Humbert Path to SharePoint http://blog.PathToSharePoint.com User Managed Solutions http://UserManagedSolutions.com


    Thursday, September 19, 2013 9:17 PM
  • I was able to get the following to work:

    POST https://<site>/_api/web/Lists/GetByTitle('[list name]')/GetItems(query=@v1)?@v1={"ViewXml":"<View><Query>[other CAML query elements]</Query></View>"}

    Hope it helps you!

    Erik

    Hi Eric

    I was wondering if this approach would get around the issues with filtering taxonomy fields. I tried this out, and got 400 Bad Request errors. I'm not a developer so its likely I'm not formatting/encoding things properly. I was wondering if you could paste what the URL should look like to do something basic like CAML query a list and match anything where the title field equals some value.

    regards

    Paul

    Saturday, September 21, 2013 1:45 AM
  • Paul, this is a different question and you might want to start a separate thread (posting your non-working code could also help). For the record, there is an example of CAML query in the question itself.

    Christophe


    Christophe Humbert Path to SharePoint http://blog.PathToSharePoint.com User Managed Solutions http://UserManagedSolutions.com

    Sunday, September 22, 2013 6:29 PM
  • While its a different question no example was provided and furthermore, google will still send people to this thread. So for the benefit of others, here is the answer as I worked it out eventually

    Here is an example of a taxonomy field being used within REST (which is cool since $filter cannot be used on taxonomy columns normally). This is a POST and you need to set the X-RequestDigest to do it.

    http://[site]/_api/Web/lists/getByTitle('[List]')/GetItems(<Query><Where><Eq><FieldRef%20Name='Sector'%20/><Value%20Type='TaxonomyFieldType'>Value</Value></Eq></Where></Query></View>"}

    regards

    Paul



    • Edited by Paul Culmsee Sunday, September 22, 2013 11:07 PM
    Sunday, September 22, 2013 11:04 PM
  • Thanks for sharing Paul. And that's right, thanks to this technique we can run queries that we couldn't write with regular OData query options. It's also a nice bridge to REST for people like me who have been using CAML with the legacy SOAP services.

    Christophe


    Christophe Humbert Path to SharePoint http://blog.PathToSharePoint.com User Managed Solutions http://UserManagedSolutions.com

    Monday, September 23, 2013 2:42 AM
  • Hi Erik,

    I am using SharePoint 2013 Office 365, I use the same way what you mentioned above, but i am getting 404 bad request error, find my link below

    https://sites/FAP/_api/web/Lists/GetByTitle('Onshore%20Transmittals')/GetItems(query=@v1)?@v1={"ViewXml":"<View><Query><ViewFields><FieldRef Name='FileLeafRef' /><FieldRef Name='Title' /></ViewFields><Where><Eq><FieldRef%20Name='Document_x0020_Type_x0020_Code'/><Value%20Type='TaxonomyFieldType'>A1</Value></Eq></Where></Query></View>"}

    Please help me on this, I double check with the URL everything seems okay, but i am getting the error. I searched a lot for the answer but nothing found.

    Many thanks,

    Abul

    Tuesday, December 10, 2013 12:13 PM