How to add user to user field of access point list item using REST api in sp2013?

I have a basic sharepoint list with a people field in it, now I'm trying to use rest api to add a new list item and try to set the user field to my alias, but it does not work and throws me below the error. It seems like this is a problem with the transfer of my user data, but I could not find any help on the Internet.

Can you guys help with the right way to make this call, any help would be greatly appreciated.

my code is ----------

function runAjax(){ var d = JSON.stringify({"__metadata":{"type":"SP.Data.RepositoryItem"},"Owners":"-1;# v-mynme@microsoft.com "}); jQuery.ajax({ url: "https://microsoft.sharepoint.com/sites/mysite/docstore/_api/web/lists/GetByTitle('Repository')/items(1580) ", type: "POST", headers: { "accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "X-HTTP-Method": "MERGE", "If-Match": "*" }, data:d, success: function (_a) { console.log(JSON.stringify(_a)); }, error: function (_e) { console.error(JSON.stringify(_e)); } });}runAjax(); error I am getting is 

Update error {"readyState": 4, "responseText": "{\" error \ ": {\" code \ ": \" - 1, Microsoft.SharePoint.Client.InvalidClientQueryException \ ", \" message \ ": { \ "lang \": \ "en-US \", \ "value \": \ "PrimitiveValue 'node with a non-zero value was found while trying to read the value of the navigation property; however, a "StartArray" node, a "StartObject" node, or a "PrimitiveValue" node with a zero value were expected. \ "}}}", "status": 400, "statusText": "Bad request"}

+6
source share
4 answers

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)); }); 
+7
source

The error message you receive may be misleading. In this case, although the Person / Group field is called (display or internal name of the SharePoint list field) Owners, using the REST API to update this field may be something else.

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

To find out the exact definition of a field, try using the following URL using the GET method and examine the XML return value.

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

XML might look something like this:

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

You will most likely notice that the Owners field is called something else, such as OwnersId. You will need to recover your ajax post data in order to use the exact field name and specify the user Id property instead of the user LoginName property.

+2
source

You tried to use the Claims encoding for the username. So I update these types of fields. Example: "I: 0 # .W | Contoso \ Chris"

0
source

You need to make a POST call using this syntax for your user field:

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

If you need to check what the exact name of the User column is, run your F12 Dev tools in a browser, launch Network Capture and enter your URL in the browser.

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

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

0
source

Source: https://habr.com/ru/post/944779/


All Articles