如何使用REST API在列表中获取多个People或Group列的TItles

时间:2015-01-23 06:53:50

标签: javascript rest sharepoint-2013

我是sharepoint的新手。我正在使用Sharepoint 2013。 我想使用REST API在People或Group列中检索名称和电子邮件ID。 我的列表包含两个这样的列。请帮我检索两列的标题和电子邮件ID

1 个答案:

答案 0 :(得分:3)

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

使用$expand OData运算符,您可以指定请求从用户字段的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

Name列返回TitleAssigntedTo

{
    "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用户字段用户值:

端点网址:/_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);
    });