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:'Tasks')/items

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

  <d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
  <d:Id m:type="Edm.Int32">1</d:Id>
  <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:Attachments m:type="Edm.Boolean">false</d:Attachments>
  <d:GUID m:type="Edm.Guid">1d3ed02b-603c-49ba-9c20-2aeb9eb3ff0c</d:GUID>

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):'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:

    <category term="SP.Data.UserInfoItem" scheme="" />
    <title />
      <name />
    <content type="application/xml">
        <d:Id m:type="Edm.Int32">1</d:Id>

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&" +
        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 Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s