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

标签 sharepoint sharepoint-2013

我有一个基本的共享点列表,其中包含 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/

    相关文章:

    java - 使用 Java 访问 Azure 中的 Office 365(sharepoint REST api)

    javascript - 在 JavaScript 数组中搜索与字符串模式匹配的所有项目的计数

    sharepoint - 如何找到我的 SharePoint Online 帐户的租户 ID

    c# - 使用 DataContext 在 Sharepoint Webpart 上抛出 401 未经授权的异常

    javascript - 使用 dd-mm-yyyy 格式的 SharePoint 列表过滤器

    c# - 根据 GUID 获取站点 URL? (分享点)

    iphone - Ios Sharepoint 集成 [sharepoint 作为后端]

    visual-studio - 创建 SharePoint 2013 查找列表列 Visual Studio 2012 列表设计器

    sharepoint - 如何将多个员工博客中的条目合并到一个 RSS 提要中

    c# - Sharepoint 客户端 GetFolderByServerRelativeUrl 文件夹修改日期