如何使用 REST API 获取列表中多个人或组列的标题

How to get TItles of multiple People Or Group column's in list using REST API

我是分享点的新手。我正在使用 Sharepoint 2013。 我想使用 REST API 在人员或组列中检索姓名和电子邮件 ID。 我的列表包含两个这样的列。请帮助我检索两列的标题和电子邮件 ID

如何使用 SharePoint REST 检索用户字段值

使用 $expand OData 运算符,您可以指定请求 returns 从 User Information List 列表中为用户字段投影字段。

ListItem 资源端点https://[site]/_api/web/lists/getbytitle('<list title>')/items(<item id>)?$select=<user field name>/Name,<user field name>/EMail&$expand=<user field name>

例子

假设一个 Tasks 列表包含 AssignedTo多值)和 Author单值) 用户字段。

第一个示例演示如何检索 AssignedTo 列用户详细信息:

/_api/web/lists/getbytitle('Tasks')/items(1)?$select=AssignedTo/Name,AssignedTo/EMail&$expand=AssignedTo

returns NameTitle 对于 AssigntedTo 列:

{
    "d": {
        "__metadata": {
            "id": "764f494a-7186-4b83-9db0-2bcf1a0930a5",
            "uri": "https://contoso.sharepoint.com/_api/Web/Lists(guid'71284427-d86e-424f-ae07-2e0c53b9ac4a')/Items(1)",
            "etag": "\"3\"",
            "type": "SP.Data.TasksListItem"
        },
        "AssignedTo": {
            "results": [
                {
                    "__metadata": {
                        "id": "a06b28ff-9356-4aa9-8f38-f75107058fd2",
                        "type": "SP.Data.UserInfoItem"
                    },
                    "Name": "i:0#.f|membership|username@contoso.onmicrosoft.com",
                    "EMail": "username@contoso.onmicrosoft.com"
                }
            ]
        }
    }
}

以下示例演示如何检索 AuthorAssignedTo 用户字段用户值:

端点Url:/_api/web/lists/getbytitle('Tasks')/items(1)?$select=Author/Name,Author/EMail,AssignedTo/Name,AssignedTo/EMail&$expand=AssignedTo,Author

结果:

{
    "d": {
        "__metadata": {
            "id": "e29690e4-3813-44ce-a828-160ad072666d",
            "uri": "https://contoso.sharepoint.com/_api/Web/Lists(guid'71284427-d86e-424f-ae07-2e0c53b9ac4a')/Items(1)",
            "etag": "\"3\"",
            "type": "SP.Data.TasksListItem"
        },
        "Author": {
            "__metadata": {
                "id": "6dc8fe57-1865-464f-aaa3-f7b8bb555f20",
                "type": "SP.Data.UserInfoItem"
            },
            "Name": "i:0#.f|membership|username@contoso.onmicrosoft.com",
            "EMail": "username@contoso.onmicrosoft.com"
        },
        "AssignedTo": {
            "results": [
                {
                    "__metadata": {
                        "id": "b9a1d6f8-4bec-4ec8-b940-fdaeac2eff37",
                        "type": "SP.Data.UserInfoItem"
                    },
                    "Name": "i:0#.f|membership|username@contoso.onmicrosoft.com",
                    "EMail": "username@contoso.onmicrosoft.com"
                }
            ]
        }
    }
}

JavaScript 例子

function getItemDetails(webUrl,listTitle,itemId,selectFields, expandFields){
    var endpointUrl = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + itemId + ")";
    endpointUrl+= '?$select=' + selectFields.join(",");
    endpointUrl+= '&$expand=' + expandFields.join(",");
    return executeRequest(endpointUrl,'GET');
}


function executeRequest(url,method,headers,payload) 
{
    if (typeof headers == 'undefined'){
        headers = {};
    }
    headers["Accept"] = "application/json;odata=verbose";
    if(method == "POST") {
        headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
    }   

    var ajaxOptions = 
    {       
       url: url,   
       type: method,  
       contentType: "application/json;odata=verbose",
       headers: headers
    };
    if(method == "POST") {
      ajaxOptions.data = JSON.stringify(payload);
    }  

    return $.ajax(ajaxOptions);
}



var webUrl = _spPageContextInfo.webAbsoluteUrl;
var selectFields = ['Author/Name','Author/EMail','AssignedTo/Name','AssignedTo/EMail'];
var expandFields = ['Author','AssignedTo'];
getItemDetails(webUrl,'Tasks',2,selectFields,expandFields)
    .done(function(data){
         //print MULTI-valued user field: AssignedTo
         console.log('AssignedTo user field value:')
         for(var i = 0; i< data.d.AssignedTo.results.length;i++) {
             console.log(data.d.AssignedTo.results[i].EMail); 
             console.log(data.d.AssignedTo.results[i].Name);       
         }

         //print SINGLE-valued user field: Author 
         console.log('Author user field value:')
         console.log(data.d.Author.EMail);
         console.log(data.d.Author.Name);
    });