4

Using REST Api, how would I go about updating a plain number column within a list? Is there any faster way of doing so aside from doing a GET, then a separate POST?

Seems there could be a faster way to do so possibly?

7

You could just use a single POST request in theory (it's actually X-http PATCH or MERGE in the case of updating a particular field in an item), but you would need to know what item you were trying to update first since you have to post to that item's endpoint... Sometimes you can get this from a selected item in a list view, otherwise you would need to just 'know' the ID of the item you were trying to update.

I usually just use jQuery to manage my AJAX requests; updating a number column is pretty straightforward, but the below assumes you're performing the update from a page within the same farm (so you don't need to fetch new credentials)

$.ajax({
    url: "/_api/lists/getByTitle('Test')/items(1)",
    type: "POST",
    data: JSON.stringify({
        '__metadata': {
            // NOTE: it's really SUPER IMPORTANT that you modify this 'type'
            // to match whatever list you're using, see MSDN for examples
            'type': 'SP.Data.TestListItem'
        },
        'yourNumberField': 512
    }),
    headers: {
        "IF-MATCH": "*",
        "X-HTTP-Method":"PATCH",
        "accept": "application/json;odata=verbose",
        "content-type": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    }
});

Here I'm setting the value to '512' but you can set it to whatever number obviously, just wanted to show that the value can be an actual number type in JS.

I mention it in comments in the code, but you'll need to change that type attribute as noted on the MSDN 'SharePoint 2013 REST Service - Working with lists and list items' page:

To do this operation, you must know the ListItemEntityTypeFullName property of the list and pass that as the value of type in the HTTP request body.

You can find this particular property by making a REST call against the list you're trying to write to...

The __REQUESTDIGEST DOM element is found on SharePoint hosted app pages and other pages in SharePoint that use the default master pages. It's a hidden element that contains a token value specific to the user viewing the page, the site they're on, and the current time -- this is called a request digest or form digest. The request digest expires after a set amount of time (the default is 30 minutes). SharePoint (and ASP applications in general) use this technique to prevent the possibility of someone spoofing a request on behalf of another user (a common form of a replay attack).

The easiest way to get a form digest on a SP hosted app or default page is by looking up the value of the DOM element with the id '__REQUESTDIGEST' (that's two leading underscores). You can also get a new form digest using the REST API by making a POST request to _api/contextinfo as described further in this MSDN magazine article.

  • I would also recommend this great article that explain how to perform CRUD operations using SharePoint 2013 REST API blogs.msdn.com/b/uksharepoint/archive/2013/02/22/… – Vadim Gremyachev Aug 26 '14 at 20:59
  • One question however, is an explanation of "X-RequestDigest": $("#__REQUESTDIGEST").val() – cm2 Aug 28 '14 at 19:40
  • Updated to include what the requestdigest is all about – John-M Aug 28 '14 at 20:43
  • this format worked, just a few minor changes like "X-HTTP-Method":"MERGE" – mcv Dec 4 '18 at 20:44

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.