可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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"}
回答1:
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));
});
回答2:
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.
回答3:
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).
回答4:
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"
回答5:
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)