如何在sp2013中使用REST api将用户添加到sharepoint列表项用户字段?

nbo*_*jja 8 sharepoint sharepoint-2013

我有一个基本的sharepoint列表,其中包含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(); 






error I am getting is 
Run Code Online (Sandbox Code Playgroud)

更新失败{"readyState":4,"responseText":"{\"error \":{\"code \":\" - 1,Microsoft.SharePoint.Client.InvalidClientQueryException \",\"message \":{ \"lang \":\"en-US \",\"value \":\"尝试读取导航属性的值时,找到了具有非空值的'PrimitiveValue'节点; 但是,期望"StartArray"节点,"StartObject"节点或具有空值的"PrimitiveValue"节点.\"}}}","status":400,"statusText":"错误请求"}

Vad*_*hev 12

如何使用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()
       }  
    });
}
Run Code Online (Sandbox Code Playgroud)

用户字段值的格式:

  • 单值用户字段: '<user field name>' : <user id>
  • 多值用户字段: '<user field name>' : { 'results': [<array of user ids>] }

多个用户字段值

该示例演示了如何创建任务项并指定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));
});
Run Code Online (Sandbox Code Playgroud)

单用户字段值

该示例演示了如何创建任务项并指定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));
});
Run Code Online (Sandbox Code Playgroud)

  • 您的回答一切都很好,但是重要的是要提到,作为开发人员,您应该在名字的末尾添加“ Id”。例如,提交的“所有者”应成为“ OwnersId”,在您的示例中,“ AssignedTo”应成为“ AssignedToId”。从头开始并不明显,请相信我:) (2认同)

小智 2

您收到的错误消息可能会令人困惑。在本例中,虽然“人员/组”字段被命名为(SharePoint 列表字段的显示或内部名称)“所有者”,但当使用 REST API 更新此字段时,它可能是其他名称。

{"__metadata":{"type":"SP.Data.RepositoryItem"},"Owners":"-1;#vmynme@microsoft.com"}
Run Code Online (Sandbox Code Playgroud)

要找出确切的字段定义,请尝试使用 GET 方法访问以下 URL 并调查返回的 XML 值。

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

XML 可能类似于以下内容

<m:properties> 
...
<d:DateModified m:type="Edm.DateTime">2014-01-07T00:00:00Z</d:DateModified><d:OwnersById m:null="true" />
...
</m:properties>
Run Code Online (Sandbox Code Playgroud)

您很可能会注意到“Owners”字段被命名为其他名称,例如“OwnersId”。您将需要重建 ajax 发布数据以使用确切的字段名称,并指定用户的Id属性而不是用户的LoginName属性作为发布值。