How to set a user field value using the SharePoint REST API
Assume that you use the following function to create a list item using SharePoint REST:
function createListItem(webUrl,listName,itemProperties) { return $.ajax({ url: webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", type: "POST", processData: false, contentType: "application/json;odata=verbose", data: JSON.stringify(itemProperties), headers: { "Accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val() } }); }
User field value format:
- single user field:
'<user field name>' : <user id> - multi-valued user field:
'<user field name>' : { 'results': [<array of user ids>] }
User field multiple value
This example shows how to create a task item and specify the multi -valued AssignedTo field:
//Create a Task item var taskProperties = { '__metadata' : { 'type': 'SP.Data.TasksListItem' }, 'Title': 'Order approval', 'AssignedToId' : { 'results': [10] } }; createListItem(_spPageContextInfo.webAbsoluteUrl,'Tasks',taskProperties) .done(function(data) { console.log('Task has been created successfully'); }) .fail( function(error){ console.log(JSON.stringify(error)); });
Single User Field Value
This example shows how to create a task item and specify a single- value field AssignedTo :
//Create a Task item var taskProperties = { '__metadata' : { 'type': 'SP.Data.TasksListItem' }, 'Title': 'Order approval', 'AssignedToId' : 10 }; createListItem(_spPageContextInfo.webAbsoluteUrl,'Tasks',taskProperties) .done(function(data) { console.log('Task has been created successfully'); }) .fail( function(error){ console.log(JSON.stringify(error)); });
source share