SharePoint REST API – getting user information from list item field

If you are using the SharePoint REST API to fetch list items, you might need to present the user information in a user field of the list item. If you for example have a Task List and want to present the Assigned To field, you will need to build the REST Query in a certain way. Consider doing the following REST query:

http://company.dev/_api/web/lists/getbytitle('Tasks')/items

The result from this query could look like this (only m:properties shown here):


<m:properties>
  <d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
  <d:Id m:type="Edm.Int32">1</d:Id>
  <d:ContentTypeId>0x010800746AAE8CF9EC4E3795D2CB9041E8CD5200460AE3B5CE4ED1499D8D5994F3E0A166</d:ContentTypeId>
  <d:Title>Test broken leg action</d:Title>
  <d:StartDate m:type="Edm.DateTime">2015-03-30T22:00:00Z</d:StartDate>
  <d:TaskDueDate m:type="Edm.DateTime">2015-04-01T22:00:00Z</d:TaskDueDate>
  <d:AssignedToId m:type="Edm.Int32">1</d:AssignedToId>
  <d:PercentComplete m:null="true" />
  <d:Body>&lt;div class="ExternalClass42AB6A0223A74082A8EE270107D0DD4F"&gt;&lt;p&gt;​Bla bla bla&lt;/p&gt;&lt;/div&gt;</d:Body>
  <d:PredecessorsId m:type="Collection(Edm.Int32)" />
  <d:Priority>(2) Normal</d:Priority>
  <d:TaskStatus>In Progress</d:TaskStatus>
  <d:RelatedItems m:null="true" />
  <d:IssueId m:type="Edm.Int32">26</d:IssueId>
  <d:ID m:type="Edm.Int32">1</d:ID>
  <d:Modified m:type="Edm.DateTime">2015-03-31T14:14:29Z</d:Modified>
  <d:Created m:type="Edm.DateTime">2015-03-31T14:14:29Z</d:Created>
  <d:AuthorId m:type="Edm.Int32">1</d:AuthorId>
  <d:EditorId m:type="Edm.Int32">1</d:EditorId>
  <d:OData__UIVersionString>1.0</d:OData__UIVersionString>
  <d:Attachments m:type="Edm.Boolean">false</d:Attachments>
  <d:GUID m:type="Edm.Guid">1d3ed02b-603c-49ba-9c20-2aeb9eb3ff0c</d:GUID>
</m:properties>

As you can see from this example, the Assigned To field is returned via the AssignedToId field. The solution to get more information about the user is to utilize the OData $expand operator together with the $select operator as in the following example (I have also selected a few other fields):

http://company.dev/_api/web/lists/getbytitle('Tasks')/items/?$select=ID,Title,Body,Issue/Id,TaskDueDate,Created,AssignedTo/FirstName,AssignedTo/LastName,AssignedTo/Name,AssignedTo/Id&$expand=Issue/Id,AssignedTo/Id&$filter=Issue/Id%20eq%2026

The AssignedTo part of the return xml could then look like this:

<m:inline>
  <entry>
    <id>59b339bc-fa37-4da7-9f56-42bfa1a32c47</id>
    <category term="SP.Data.UserInfoItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <title />
    <updated>2015-04-01T06:38:45Z</updated>
    <author>
      <name />
    </author>
    <content type="application/xml">
      <m:properties>
        <d:FirstName>John</d:FirstName>
        <d:LastName>Doe</d:LastName>
        <d:Name>i:0#.w|glenny\john.doe</d:Name>
        <d:Id m:type="Edm.Int32">1</d:Id>
      </m:properties>
    </content>
  </entry>
</m:inline>

In many cases, you would also add the $filter operator to filter out a sub set of the list items. Now, by using jQuery (or other types of AJAX calls), we can query the Task list like this:

var getTasks = function () {
    var promise = $.ajax({
        url: "/_api/web/lists/getbytitle('Tasks')/items/?" + 
            "$select=ID,Title,Body,Issue/Id,TaskDueDate,Created,AssignedTo/FirstName,AssignedTo/LastName,AssignedTo/Name,AssignedTo/Id&" +
            "$expand=Issue/Id,AssignedTo/Id",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
    });
    return promise;
};

The asynchronous function returns a promise which could be used like this:

var promiseTasks = getTasks();
$.when(promiseTasks).done(function (xhrTasks) {
    var tasks = xhrTasks.d.results;
    $.each(tasks, function (index, task) {

        // Get the user information
        var assignedToFullName = task.AssignedTo.FirstName + ' ' + task.AssignedTo.LastName;
        var assignedToUserName = task.AssignedTo.Name;

        // Do something with the information
        doSomethingWithTheTask(task.Title, assignedToFullName, AssignedToUserName);

    });
});

Leave a comment