我有一个基本的sharepoint列表与人在它的字段,现在我正在尝试使用rest api添加新的列表项目,并试图设置我的别名的人字段,但它不工作,并抛出我在错误下面。看起来我的用户数据传递方式有问题,但我在网上找不到任何帮助。
你们可以帮助正确的方式打这个电话吗,任何帮助都非常感谢。
我的代码
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"}
发布于 2014-12-23 04:14:50
如何使用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>] }
,
多用户字段值
示例演示如何创建任务项并指定multi-valued AssignedTo
字段:
//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-valued 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));
});
发布于 2014-01-09 05:32:05
您收到的错误消息可能会令人困惑。在本例中,尽管Person/Group字段被命名为"Owners“( SharePoint列表字段的显示或内部名称),但在使用REST API更新此字段时,它可能是其他名称。
{"__metadata":{"type":"SP.Data.RepositoryItem"},"Owners":"-1;#vmynme@microsoft.com"}
要找出确切的字段定义,请使用GET方法尝试以下URL并检查返回的XML值。
https://microsoft.sharepoint.com/sites/mysite/docstore/_api/web/lists/GetByTitle('Repository')/items(1580)
XML可能如下所示
<m:properties>
...
<d:DateModified m:type="Edm.DateTime">2014-01-07T00:00:00Z</d:DateModified><d:OwnersById m:null="true" />
...
</m:properties>
您很可能会注意到,字段"Owners“被命名为其他名称,例如"OwnersId”。您将需要重新构造ajax post数据以使用确切的字段名称,并指定用户的Id属性而不是用户的LoginName属性作为发布的值。
发布于 2018-07-05 14:02:51
正确的方法应该如下所示:(我相信总有一天它会救人的)
var requestApprover = {
'__metadata' : { 'type': 'Collection(Edm.Int32)' },
'results': [10,20,30]
};
请记住,字段名称应该在末尾包含Id,类型应该是您正在更新的字段类型,其中对于个人或组(多个)是集合(Edm.Int32)。
https://stackoverflow.com/questions/16500077
复制相似问题