1
votes

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!

2
What do you mean by "certain", could you please update your question and specify, for example, field type?Vadim Gremyachev
@VadimGremyachev I have updated by question. I hope I made it very clear.Syed Mauze Rehan
See my ultimate goal is to update the sharing details for items through REST APISyed Mauze Rehan

2 Answers

1
votes

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.

0
votes

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