如何使用REST api获取“创建者”的SharePoint列表属性

时间:2015-01-21 06:57:17

标签: rest sharepoint sharepoint-2013

我需要使用REST API在项目中显示SharePoint列表的创建者。

当我使用

/_api/web/lists/getbytitle('Employee')

我得到了回应但是,我没有找到Created By Field。 我如何知道谁创建了列表。 这是我的回复:

"AllowContentTypes": true,
"BaseTemplate": 100,
"BaseType": 0,
"ContentTypesEnabled": false,
"CrawlNonDefaultViews": false,
"Created": "2014-12-03T06:58:18Z",
"DefaultContentApprovalWorkflowId": "00000000-0000-0000-0000-000000000000",
"Description": "",
"Direction": "none",
"DocumentTemplateUrl": null,
"DraftVersionVisibility": 0,
"EnableAttachments": true,
"EnableFolderCreation": false,
"EnableMinorVersions": false,
"EnableModeration": false,
"EnableVersioning": false,
"EntityTypeName": "EmployeeList",
"ForceCheckout": false,
"HasExternalDataSource": false,
"Hidden": false,
"Id": "13cdc8f4-7fed-42eb-9a38-08c30eec6a87",
"ImageUrl": "/_layouts/15/images/itgen.png?rev=38",
"IrmEnabled": false,
"IrmExpire": false,
"IrmReject": false,
"IsApplicationList": false,
"IsCatalog": false,
"IsPrivate": false,
"ItemCount": 2,
"LastItemDeletedDate": "2014-12-03T06:58:18Z",
"LastItemModifiedDate": "2015-01-21T05:54:31Z",
"ListItemEntityTypeFullName": "SP.Data.EmployeeListItem",
"MajorVersionLimit": 0,
"MajorWithMinorVersionsLimit": 0,
"MultipleDataList": false,
"NoCrawl": false,
"ParentWebUrl": "/",
"ParserDisabled": false,
"ServerTemplateCanCreateFolders": true,
"TemplateFeatureId": "00bfea71-de22-43b2-a848-c05709900100",
"Title": "Employee"

感谢。

1 个答案:

答案 0 :(得分:4)

在服务器端API(SSOM)和客户端API(REST或CSOM)中,对象通常不会公开同一组属性。这与List资源完全相同,其中Author属性在CSOM和REST中不可用,但在SSOM中可用。

您可以考虑使用以下方法通过REST检索Author资源的List属性。我们的想法是从List Schema中获取此属性,如下所示(*):

  • 第一步是检索列表架构: /_api/web/lists/getbytitle('list title')?$select=schemaXml
  • 然后您可以从SchemaXml属性中提取作者ID
  

(*)列表SchemaXml属性存储Author ID属性

示例

function getListAuthor(webUrl,listTitle)
{
    var endpointUrl = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')?$select=schemaXml";
    return executeRequest(endpointUrl,'GET').then(function(data){
             var listProperties = schemaXml2Json(data.d.SchemaXml);
             return parseInt(listProperties.Author);
           });
}


//Usage: Retrieve List Author Id
getListAuthor(_spPageContextInfo.webAbsoluteUrl,'Contacts')
.done(function(authorId){
    console.log('List Author Id: ' + authorId);

});

如果List Author Id属性不够并且您想要检索Author用户,那么您可以使用以下示例:

var webUrl = _spPageContextInfo.webAbsoluteUrl;
getListAuthor(webUrl,'Contacts')
.then(function(authorId){
    getSiteUser(webUrl,authorId)
    .done(function(data){
         var listAuthor = data.d;
         console.log(listAuthor);
    });     
}); 

,其中

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);
}


function schemaXml2Json(schemaXml)
{ 
    var jsonObject = {};
    var schemaXmlDoc = $.parseXML(schemaXml);
    $(schemaXmlDoc).find('List').each(function() {
      $.each(this.attributes, function(i, attr){
           jsonObject[attr.name] = attr.value;
      });
    });
    return jsonObject;
}


function getSiteUser(webUrl,userId){
    var endpointUrl = webUrl + "/_api/web/siteUsers/getById(" + userId +  ")";
    return executeRequest(endpointUrl,'GET');
}

关注this post了解更多详情。