how to add user to sharepoint list item user field using REST api in sp2013?

落花浮王杯 提交于 2019-12-03 03:54:13

How to set User field value using SharePoint REST API

Assume the following function is used for creating 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()
       }  
    });
}

The format for user field value:

  • single-valued user field: '<user field name>' : <user id>
  • multi-valued user field: '<user field name>' : { 'results': [<array of user ids>] }

Multiple user field value

The example demonstrates how to create a task item and specify 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

The example demonstrates how to create a task item and specify a single-valued AssignedTo field:

//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));
});

The error message you are receiving can be confusing. In this case, though the Person/Group field is named (display or internal name of the SharePoint list field) "Owners", when using REST API to update this field, it may be something else.

{"__metadata":{"type":"SP.Data.RepositoryItem"},"Owners":"-1;#vmynme@microsoft.com"}

To find out the exact field definition, try the following URL using GET method and investigate the XML value returned.

https://microsoft.sharepoint.com/sites/mysite/docstore/_api/web/lists/GetByTitle('Repository')/items(1580)

The XML may look something like the following

<m:properties> 
...
<d:DateModified m:type="Edm.DateTime">2014-01-07T00:00:00Z</d:DateModified><d:OwnersById m:null="true" />
...
</m:properties>

Most likely you will notice that the field "Owners" is named something else, such as "OwnersId". You will need to reconstruct your ajax post data to use the exact field name and specify the user's Id property instead of user's LoginName property as the posted value.

The right way should be as shown below: (I am sure it will save someone sometime)

 var requestApprover = {
                        '__metadata' : { 'type': 'Collection(Edm.Int32)' },
                        'results': [10,20,30]
                    };

Remember the field name should contain Id at the end and the type should be the field type that you are updating where for people or groups (multiple) is Collection(Edm.Int32).

Have you tried to use the Claims Encoding of the username. That is the way I update such types of fields. Example: "i:0#.w|contoso\chris"

You need to perform the POST call using this syntax for your User Field:

If field has name "Owners", you need to send "OwnersId": 1 inside your REST call body (Append ID to your column name and pass the value of UserID).

If you need to check what is the exact name of the User column, start your F12 Dev Tools in the browser, Start Network Capture and then type in your URL in the browser.

"https://microsoft.sharepoint.com/sites/mysite/docstore/_api/web/lists/GetByTitle('Repository')/items(1580) "

In the response body, you will see exact names of all columns (as used in REST)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!