2015-01-21 116 views
0

我需要使用REST API在项目中显示SharePoint列表的创建者。如何使用REST API获取SharePoint列表属性'创建者'

当我使用

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

我得到回应,但,我不找创建现场。 我如何知道谁创建了该列表。 这是我的回应:

"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" 

谢谢。

回答

3

在服务器端API(SSOM)和客户端API(REST或CSOM)的对象通常不暴露所述相同的属性集。列表资源就是这种情况,其中Author属性在CSOM和REST中不可用,但在SSOM中可用。

您可以考虑采用以下方法通过REST检索List资源的Author属性。这个想法是从列表架构得到这个属性,如下面所示(*):

  • 第一步是检索列表架构: /_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); 

}); 

如果列表作者Id属性是不够的,你想获取作者的用户,那么你可以利用下面的例子:

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了解更多详情。

+0

是这样我就可以通过REST 2调用获得作者(从schema获取userid,然后通过另一个cal获取用户名)。但我需要与作者一起显示网站中的所有列表。 现在想象一下我可能需要拨打的电话数量:( – Vikas 2015-01-22 04:34:03

+0

)由于这个问题是关于获取Author属性而没有其他,所以我建议你发布另一个问题我很确定我们可以找到一种方法使用作者属性加载有效列表 – 2015-01-24 09:01:41

相关问题