I have a basic sharepoint list with people field in it, now I am trying to use rest api to add new list item and trying to set person field to my alias but it is not working and throwing me below error. looks like it is problem with how my user data is passed but I could not able to find any help online.
can you guys help with the correct way to make this call, any help is greatly appreciated.
my Code----------
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
Updation fail{"readyState":4,"responseText":"{\"error\":{\"code\":\"-1, Microsoft.SharePoint.Client.InvalidClientQueryException\",\"message\":{\"lang\":\"en-US\",\"value\":\"A 'PrimitiveValue' node with non-null value was found when trying to read the value of a navigation property; however, a 'StartArray' node, a 'StartObject' node, or a 'PrimitiveValue' node with null value was expected.\"}}}","status":400,"statusText":"Bad Request"}
How to set User field value using SharePoint REST API
Assume the following function is used for creating a list item using SharePoint REST:
The format for user field value:
'<user field name>' : <user id>
'<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:Single user field value
The example demonstrates how to create a task item and specify a single-valued
AssignedTo
field: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.
In the response body, you will see exact names of all columns (as used in REST)
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"
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.
To find out the exact field definition, try the following URL using GET method and investigate the XML value returned.
The XML may look something like the following
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)
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).