我有一个基本的共享点列表,其中包含 people 字段,现在我正在尝试使用 rest api 添加新列表项并尝试将 person 字段设置为我的别名,但它不起作用并将我抛出以下错误。看起来我的用户数据的传递方式有问题,但我无法在线找到任何帮助。
你们能帮我用正确的方法打这个电话吗,非常感谢任何帮助。
我的代码----------
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();
我得到的错误是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"}
最佳答案
如何使用 SharePoint REST API 设置用户字段值
假设以下函数用于使用 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 name>' : <user id>
'<user field name>' : { 'results': [<array of user ids>] }
多用户字段值
该示例演示了如何创建任务项并指定 多 -值
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));
});
单用户字段值
该示例演示了如何创建任务项并指定 单例 -值
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));
});
关于sharepoint - 如何在 sp2013 中使用 REST api 将用户添加到共享点列表项用户字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16500077/