1

I want to update certain fields for a ListItem in Sharepoint. How can I achieve this via the REST API?

Is there any documentation for this?

EDIT 1

Consider the following JSON that I receive on requesting a ListItem(GET)

{
"d": {
    "__metadata": {
        "id": "8b3d2196-ad3e-4766-a23e-7e6a89153965",
        "uri": "https://abc-my.sharepoint.com/personal/nn_abc_co/_api/Web/Lists(guid'd02a8c5b-cd69-4aa8-b7fb-9d539733f285')/Items(383)",
        "etag": "\"2\"",
        "type": "SP.Data.DocumentsItem"
    },
    "FirstUniqueAncestorSecurableObject": {
        "__deferred": {
            "uri": ".../FirstUniqueAncestorSecurableObject"
        }
    },
    "RoleAssignments": {
        "__deferred": {
            "uri": ".../RoleAssignments"
        }
    },
    "AttachmentFiles": {
        "__deferred": {
            "uri": ".../AttachmentFiles"
        }
    },
    "ContentType": {
        "__deferred": {
            "uri": ".../ContentType"
        }
    },
    "FieldValuesAsHtml": {
        "__deferred": {
            "uri": ".../FieldValuesAsHtml"
        }
    },
    "FieldValuesAsText": {
        "__metadata": {
            "id": ".../FieldValuesAsText",
            "uri": ".../_api/Web/Lists(guid'd02a8c5b-cd69-4aa8-b7fb-9d539733f285')/Items(383)/FieldValuesAsText",
            "type": "SP.FieldStringValues"
        },
        "ContentTypeId": "0x0120000E7EB3018A9074468823208C432BDCA5",
        "Title": "",
        "IsMyDocuments": "",
        "SharedWithInternal": "",
        "SharedWithUsers": "",
        "ID": "383",
        "Created": "7/29/2014 11:28 PM",
        "Author": "3",
        "Modified": "7/29/2014 11:28 PM",
        "Editor": "3",
        "OData__x005f_CopySource": "",
        "CheckoutUser": "",
        "OData__x005f_UIVersionString": "1.0",
        "GUID": "da1e223b-1335-49e8-a544-b2cbebd4064f",
        "OData__x005f_ModerationStatus": "Approved",
        "OData__x005f_Level": "1",
        "UniqueId": "3dba6291-92c7-458f-9a28-e0a91696d9ca",
        "FSObjType": "1",
        ...
        },
    "FileSystemObjectType": 1,
    "Id": 383,
    "ContentTypeId": "0x0120000E7EB3018A9074468823208C432BDCA5",
    "Title": null,
    "IsMyDocuments": null,
    "SharedWithInternalId": null,
    "SharedWithUsersId": {
        "__metadata": {
            "type": "Collection(Edm.Int32)"
        },
        "results": [5]
       },
...   }
}

From the JSON above(which I have altered ofcourse, but is a real response), I need to change the value for "SharedWithUsers" under "FieldValuesAsText" object.

Is this possible? I have been trying it, and even the call returns success response code, but it doesn't change anything.

On the other hand if I change fields in the first level, by that I mean fields like "Title", it changes successfully.

However(beyond my understanding) values like "SharedWithUsersId" in the first level don't change either. Even though I get the same success response code for it as well.

The Post data I am creating is as follows,

For Title (works like a charm! Every blog that I have read, refers this example!)

{
  "__metadata": {
    "type": "SP.Data.DocumentsItem"
},
    "Title":"Blabla"
}

For SharedWithUsers field under FieldValuesForEdit OR FieldValuesAsText

{
"__metadata": {
    "type": "SP.Data.DocumentsItem"
},
"FieldValuesForEdit": {
    "__metadata": {
        "type": "SP.FieldStringValues"
    },
    "SharedWithUsers": ""
}
}

I have verified that SharedWithUsers's value is "" when it doesn't contain any data (the sample JSON i have posted confirms it) But POST call with such data doesn't update it.

For SharedWithUsersId

{
"__metadata": {
    "type": "SP.Data.DocumentsItem"
},
"SharedWithUsersId": {
        "__metadata": {
            "type": "Collection(Edm.Int32)"
        },
        "results": []
    }
}

Note here that in this case >>> "SharedWithUsersId":null doesn't work either, even though my own sinful eyes have seen such a response for some items! (All Hail Microsoft!!!)

For all of the above POST data JSON objects, I get a 204 response code (which I believe is the right code for a MERGE call)

I hope I have explained my question well enough. Any or all help is appreciated!

3
  • What do you mean by "certain", could you please update your question and specify, for example, field type? Commented Sep 2, 2014 at 13:28
  • @VadimGremyachev I have updated by question. I hope I made it very clear. Commented Sep 2, 2014 at 17:58
  • See my ultimate goal is to update the sharing details for items through REST API Commented Sep 2, 2014 at 18:02

2 Answers 2

2

Read this MSDN post: http://msdn.microsoft.com/en-us/library/office/dn292552%28v=office.15%29.aspx#ListItems

url ==> should be pointing to the listitem you want to update.

method ==> POST, because you want to update a listitem

body ==> JSON formatted, contains the fields you want to update. Omit the ones you don't want to change.

Below and example:

url: http://site url/_api/web/lists/GetByTitle(‘Test')/items(item id)
method: POST
body: { '__metadata': { 'type': 'SP.Data.TestListItem' }, 'Title': 'TestUpdated'}
headers:
   Authorization: "Bearer " + accessToken
   X-RequestDigest: form digest value
   "IF-MATCH": etag or "*"
   "X-HTTP-Method":"MERGE",
   accept: "application/json;odata=verbose"
   content-type: "application/json;odata=verbose"
   content-length:length of post body

Getting the accessToken could be hard, depending from what kind of application (SharePoint App, Console App, inside SharePoint) you do this call.

Sign up to request clarification or add additional context in comments.

3 Comments

Yep! I've gone through this, but my problem is a bit tricky. For a Listitem there is a field called "FieldValuesAsText", and I want to update an inner field of this object. The JSON i created looked like >>> { "__metadata": { "type": "SP.Data.DocumentsItem" }, "FieldValuesAsText": { "__metadata": { "type": "SP.FieldStringValues" }, "SharedWithUsers": "" } } But I get a 204 saying No Content. One thing I noted was tha everytime i executed my call, the value for ETag incremented!
Although that works if you want to change first level field values... like ... say Title etc... but I'm looking for a way to change the values for an inner object.
have you tried: { "__metadata": { "type": "SP.Data.DocumentsItem" }, "Title":"Blabla", "SharedWithUsers": <some user id (interger) of a user> }
-1

If you wish to do this using the HttpClient you can reference this article. I show how to get the Digest and Upload files and update the list item after the fact. Works for Windows Store, Console and Forms apps.

Example for updating a file from the HttpClient using REST:

client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }); client.BaseAddress = new System.Uri(url); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Add("X-RequestDigest", digest); client.DefaultRequestHeaders.Add("X-HTTP-Method", "MERGE"); client.DefaultRequestHeaders.Add("IF-MATCH", "*"); HttpContent strContent = new StringContent(String.Concat("{ '__metadata': { 'type': 'SP.List' }, 'Title': '", filename, "' }")); strContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); strContent.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("odata", "verbose")); HttpResponseMessage updateResponse = await client.PostAsync(String.Concat("_api/web/lists/GetByTitle('Project Photos')/Items(", id, ")"), strContent); updateResponse.EnsureSuccessStatusCode(); if (updateResponse.IsSuccessStatusCode) {}

https://arcandotnet.wordpress.com/2015/04/01/sharepoint-2013-rest-services-using-c-and-the-httpclient-for-windows-store-apps/

Arcan.NET

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.