Skip to main content
deleted 7 characters in body
Source Link
Vadim Gremyachev
  • 42.6k
  • 3
  • 87
  • 169

Example:Example

Example

Example:

Example

Example

Post Undeleted by Vadim Gremyachev
added 643 characters in body
Source Link
Vadim Gremyachev
  • 42.6k
  • 3
  • 87
  • 169

How to retrieve List Author property using SharePoint REST Interface

By default REST query /_api/web/lists/getbytitle('<list title>')/items returns AuthrorIdList resource does not expose Author property, but you could retrieve it from List Schema as demonstrated below (User Id for Created By field*).

  • First step is to retrieve List Schema: /_api/web/lists/getbytitle('list title')?$select=schemaXml
  • Then you could extract Author Id from SchemaXml property

(*) List SchemaXml property stores Author ID property

Example:

{
    "d": {
        "results":function [getListAuthor(webUrl,listTitle)
            {
           var endpointUrl = webUrl + "/_api/remaining outputweb/lists/getbytitle('" is+ omittedlistTitle for+ clarity"')?$select=schemaXml";
                "FileSystemObjectType":return 0executeRequest(endpointUrl,'GET').then(function(data){
              var listProperties "Id":= 1,schemaXml2Json(data.d.SchemaXml);
                "ContentTypeId":return "0x010070092C478B02EF49ADA0691DE01FFBF6",parseInt(listProperties.Author);
                "Title": "John Doe",});
             }


//Usage: Retrieve List "Email":Author "jdoe@contosoId
getListAuthor(_spPageContextInfo.com"webAbsoluteUrl,'Contacts')
                "ID": 1,.done(function(authorId){
               console.log('List "Modified":Author "2015-01-21T07:33Id:21Z",
               ' "Created":+ "2015-01-21T07:33:21Z",authorId);
                "AuthorId": 17,
                "EditorId": 17,});
              

If List Author Id property is not enough and you would like to retrieve Author user, then you could utilize the following example:

var webUrl "OData__UIVersionString":= "1_spPageContextInfo.0"webAbsoluteUrl;
getListAuthor(webUrl,'Contacts')
             .then(function(authorId){
   "Attachments": falsegetSiteUser(webUrl,authorId)
     .done(function(data){
         var listAuthor "GUID":= "01e58248-b0fc-4353-ad59-64010d9bb331"data.d;
            }console.log(listAuthor);
       }); ]
    }
}

Using $expand OData operator you can specify that the request returns projected fields from other lists and the values of lookups.

The following REST endpoint:

/_api/web/lists/getbytitle('<list title>')/items?$select=Author/Title,Author/EMail&$expand=Author; 

returns Title and Email for Author(Created By) column in list:where

{
   function "d":executeRequest(url,method,headers,payload)  
{
     if (typeof headers "results":== ['undefined'){
          headers = {};
    }
    headers["Accept"] = "application/json;odata=verbose";
    if(method == "__metadata":"POST") {
                    "id": "07849f26-815c-40df-ba3dheaders["X-efaac658c066",
                   RequestDigest"] "uri":= "https://contoso.sharepoint.com/_api/Web/Lists$(guid'81916ced-b57f-4940-bf1a-eef4af00aaee'"#__REQUESTDIGEST")/Items.val(1)",;
                 }   "etag": "\"1\"",
               
    var ajaxOptions "type":= "SP.Data.ContactsListItem"
         {       },
                "Author"url: {
url,    
       type: method,   
       "__metadata"contentType: {"application/json;odata=verbose",
                        "id"headers: "b503837d-4cce-4b8f-96a4-26341abfc39a",headers
          };
    if(method == "POST") {
      ajaxOptions.data "type":= "SP.DataJSON.UserInfoItem"stringify(payload);
             }   

    return $.ajax(ajaxOptions);
},
 

function schemaXml2Json(schemaXml)
{  
    var jsonObject = {};
    var schemaXmlDoc = $.parseXML(schemaXml);
   "Title": "John$(schemaXmlDoc).find('List').each(function() Doe",{
      $.each(this.attributes, function(i, attr){
           jsonObject[attr.name] "EMail":= "[email protected]attr.com"value;
            });
    });
    return jsonObject;
}


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

Example

By default REST query /_api/web/lists/getbytitle('<list title>')/items returns AuthrorId (User Id for Created By field):

{
    "d": {
        "results": [
            {
                //remaining output is omitted for clarity
                "FileSystemObjectType": 0,
                "Id": 1,
                "ContentTypeId": "0x010070092C478B02EF49ADA0691DE01FFBF6",
                "Title": "John Doe",
                "Email": "jdoe@contoso.com",
                "ID": 1,
                "Modified": "2015-01-21T07:33:21Z",
                "Created": "2015-01-21T07:33:21Z",
                "AuthorId": 17,
                "EditorId": 17,
                "OData__UIVersionString": "1.0",
                "Attachments": false,
                "GUID": "01e58248-b0fc-4353-ad59-64010d9bb331"
            }
        ]
    }
}

Using $expand OData operator you can specify that the request returns projected fields from other lists and the values of lookups.

The following REST endpoint:

/_api/web/lists/getbytitle('<list title>')/items?$select=Author/Title,Author/EMail&$expand=Author

returns Title and Email for Author(Created By) column in list:

{
    "d": {
        "results": [
            {
                "__metadata": {
                    "id": "07849f26-815c-40df-ba3d-efaac658c066",
                    "uri": "https://contoso.sharepoint.com/_api/Web/Lists(guid'81916ced-b57f-4940-bf1a-eef4af00aaee')/Items(1)",
                    "etag": "\"1\"",
                    "type": "SP.Data.ContactsListItem"
                },
                "Author": {
                    "__metadata": {
                        "id": "b503837d-4cce-4b8f-96a4-26341abfc39a",
                        "type": "SP.Data.UserInfoItem"
                    },
                    "Title": "John Doe",
                    "EMail": "[email protected].com"
                }
            }
        ]
    }
}

How to retrieve List Author property using SharePoint REST Interface

List resource does not expose Author property, but you could retrieve it from List Schema as demonstrated below (*).

  • First step is to retrieve List Schema: /_api/web/lists/getbytitle('list title')?$select=schemaXml
  • Then you could extract Author Id from SchemaXml property

(*) List SchemaXml property stores Author ID property

Example:

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

If List Author Id property is not enough and you would like to retrieve Author user, then you could utilize the following example:

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

where

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

Example

Post Deleted by Vadim Gremyachev
Source Link
Vadim Gremyachev
  • 42.6k
  • 3
  • 87
  • 169

By default REST query /_api/web/lists/getbytitle('<list title>')/items returns AuthrorId (User Id for Created By field):

{
    "d": {
        "results": [
            {
                //remaining output is omitted for clarity
                "FileSystemObjectType": 0,
                "Id": 1,
                "ContentTypeId": "0x010070092C478B02EF49ADA0691DE01FFBF6",
                "Title": "John Doe",
                "Email": "[email protected]",
                "ID": 1,
                "Modified": "2015-01-21T07:33:21Z",
                "Created": "2015-01-21T07:33:21Z",
                "AuthorId": 17,
                "EditorId": 17,
                "OData__UIVersionString": "1.0",
                "Attachments": false,
                "GUID": "01e58248-b0fc-4353-ad59-64010d9bb331"
            }
        ]
    }
}

Using $expand OData operator you can specify that the request returns projected fields from other lists and the values of lookups.

The following REST endpoint:

/_api/web/lists/getbytitle('<list title>')/items?$select=Author/Title,Author/EMail&$expand=Author

returns Title and Email for Author(Created By) column in list:

{
    "d": {
        "results": [
            {
                "__metadata": {
                    "id": "07849f26-815c-40df-ba3d-efaac658c066",
                    "uri": "https://contoso.sharepoint.com/_api/Web/Lists(guid'81916ced-b57f-4940-bf1a-eef4af00aaee')/Items(1)",
                    "etag": "\"1\"",
                    "type": "SP.Data.ContactsListItem"
                },
                "Author": {
                    "__metadata": {
                        "id": "b503837d-4cce-4b8f-96a4-26341abfc39a",
                        "type": "SP.Data.UserInfoItem"
                    },
                    "Title": "John Doe",
                    "EMail": "[email protected]"
                }
            }
        ]
    }
}