3

I am trying to understand the logic with the SharePoint client.svc (_api) Web service.

For some queries, for example List GetItems, or search queries, SharePoint requires the use of POST in ajax calls. Queries are just read operations, without server side updates, so why is that?

Using POST is heavy, as you need to retrieve a request digest token first. I'd rather use a simple GET.

For an example of query that requires POST, see the answer on this page: http://social.msdn.microsoft.com/Forums/sharepoint/en-US/92cccd65-ba4c-4870-a858-7cd0e38a0482/how-can-i-use-caml-queries-with-the-rest-api

  • 3
    I have several bones to pick with whoever implemented the _api endpoint. They document it as OData when it's nowhere near compliant. – James Love Oct 2 '13 at 21:33
  • @JamesLove I know, I feel your pain. One issue is that people don't necessarily implement the latest version of the standard (similar to the XSLT story). – Christophe Oct 2 '13 at 21:45
  • It's a shame that 'People' turn out really just to be 'Microsoft'. When you look at some other technologies out there, the MS stack is horrendously archaic. – James Love Oct 3 '13 at 6:44
6

From the example you have supplied it seems that you only need to use POST if you are using CAML queries. You can use a simple GET if you use the OData querying method see OData in SharePoint 2013 with jQuery and SharePoint 2013 – CRUD on List Items Using REST Services & jQuery for fuller explanations and some simple examples.

See MSDN - Use OData query operations in SharePoint REST requests for the OData query syntax used.

The quote and code below is from SharePoint 2013 – CRUD on List Items Using REST Services & jQuery.

REST SERVICES – GET LIST ITEMS Getting List Items using the SharePoint REST Services is probably the simplest operation of them all. An example of how to do this is below:

// Getting list items based on ODATA Query
function getListItems(url, listname, query, complete, failure) {

    // Executing our colors ajax request
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items" + query,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            complete(data); // Returns JSON collection of the results
        },
        error: function (data) {
            failure(data);
        }
    });    
}
  • Right, it seems that POST is related to CAML. Why would a simple GET be enough with OData, while a POST is required if the exact same request is sent in CAML format? – Christophe Oct 2 '13 at 15:10
  • 3
    Because a CAML query is a payload. SharePoint and the whole CAML thing is a bit stone-age in terms of technologies like REST - remotely getting data out with CAML was intended only really to be done via SOAP web services, back in the day. – James Love Oct 2 '13 at 21:28
  • @JamesLove the issue is that 1/ there are still many queries that cannot be done via OData (cf. example in the question) and 2/ if you retrieve a view query using REST you'll get it in... CAML format! – Christophe Oct 3 '13 at 4:58
0

I didn't find a definitive answer, but as I was exploring the REST search api I found some elements that could explain the need for POST.

The search api allows both GET and POST. POST requests are recommended in the following scenarios:

  • When you'll exceed the URL length restriction with a GET request.
  • When you can't specify the query parameters in a simple URL. For example, if you have to pass parameter values that contain a complex type array, or comma-separated strings, you have more flexibility when constructing the POST request.
  • When you use the ReorderingRules parameter because it is supported only with POST requests.

My assumption: with requests that include CAML queries, like GetItems, there's a significant risk to exceed the URL length restriction (well, at least in older browsers), and that's why GET requests are not allowed.

[Update] the url limitations for Internet Explorer are detailed here: http://support.microsoft.com/kb/208427

Excerpt:

the POST method is not limited by the size of the URL for submitting name/value pairs. These pairs are transferred in the header and not in the URL.

  • Also, a query issued via REST is normally in the format of ...&field=value&field2=value etc., or using the $filter statement, both of which which are not CAML. Putting CAML in a query string is just madness. – James Love Oct 2 '13 at 21:30
  • @JamesLove madness or not, this is the way it works in SharePoint (cf. the example in the question). Generally speaking, a parameter value in a querystring could be a CAML expression, or JSON, or really any string you can imagine. – Christophe Oct 2 '13 at 21:35
  • 1
    "We'll give you an endpoint that kinda resembles OData, but we won't fully implement the specification, and we'll still make you learn SharePoint's query model. Call it job security". – James Love Oct 3 '13 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.