Wednesday, September 17, 2014

Updating UserMulti field using Sharepoint 2013 REST API

To update User field via Sharepoint 2013 REST API one just needs to provide ID of the user in the JSON of the element being updated.

For example, say we have list Email and it contains field Recipient which is a user field. When you query this list using Sharepoint 2013 REST API you will get RecipientId field instead. This is the clue to updating the field - you just need to provide ID of the user you want to be a recipient.



var el = { 
 "__metadata": { "type": "SP.Data.EmailListItem" },
 Title: "Title of the message", 
 Body" "Body of the message", 
 RecipientId: 5 
 };

The ID of the user cam be obtained via EnsureUser function.

When your Recipients field has type of UserMulti you need to deal with the array of user IDs. You will need to specify also metadata for the array type: Collection(Edm.Int32).

var el = { 
 "__metadata": { "type": "SP.Data.EmailListItem" },
 Title: "Title of the message", 
 Body" "Body of the message", 
 RecipientsId: { "__metadata": { "type": "Collection(Edm.Int32)" }, "results": [5,4,6] },
 };