Fetch User Details And Profile Picture From Person Or Group Field Using REST API

Introduction

In one of my projects (on Office 365), I have a requirement to scroll the key contacts of a project with the user's details - Full Name, Department, Job Title, Phone (Work/ Mobile) and Picture URL. Since all contacts are internal employees, I don’t have to input user details, I just need a person and a group field in a list to capture the user.

 

In this article, we will learn how to fetch the user details and profile URL using REST API.

Approach

I have created a custom list (Key Contacts) with a people field by the name Contact. By default, List REST API returns User ID despite the show field selected in the column properties. Most of you might have thought the selection of “Name (with picture and details)” property would return all properties along with profile URL but it will return ID only since it is a lookup field.

REST URL - https:{sitecollection}/_api/web/lists/getbytitle('{ListName}’)/items

Response

 

In order to retrieve the user details, we have to include $expand OData query option. Now, this will allow fetching entities associated for the given property in the response. Explicitly specify which details (Contact/FirstName) you would like to retrieve, apparently there is no option to select all fields (like this Contact/*).

All fields names must be case sensitive and refer to the Internal names not to the display names.

For example - Contact/email will not work, it should be Contact/EMail

REST URL

https:{sitecollection}/_api/web/lists/getbytitle('{ListName}’)/items?$select=Contact/FirstName,Contact/LastName,Contact/JobTitle,Contact/WorkPhone,Contact/EMail,Contact/UserName&$expand=Contact/Id

Response

 

Thus, you can retrieve the details of the user. However, not all properties are retuned (this is on SharePoint online ) as stated here. It says “The Query to the field <fieldname> is not successful” for few fields.

I was successful with the following.

  • Title
  • Name
  • EMail
  • MobilePhone
  • SipAddress
  • Department
  • JobTitle
  • FirstName
  • LastName
  • WorkPhone
  • UserName
  • Office
  • ID
  • Modified
  • Created

Mainly, one of my key details, “Picture URL” is missing. This really gave me a hiccup since it is the only one missing for my requirement and I didn’t want to make another user profile REST call to retrieve the URL. Anyhow, I figured out the option.

If you inspect the HTML of the profile URL in the IE developer, you can derive a URL format.

 

URL format is as follows, which takes account name (Email) as the input and Size(L/M/S) based on you design this acts as a rendition ID.

_spPageContextInfo.siteServerRelativeUrl + "/_layouts/15/userphoto.aspx?size=L&accountname=" + Email

Below are the image dimension variations,

  • L - 200px * 200px
  • M - 72px * 72px
  • S - 48px * 48px

Now, here is the final code.

Note

For carousel (scroll) functionality, insert bootstrap CSS reference. Since I added it globally in the master page, I have not included that here. 
  1. <div id="KeyContactsContainer" class="carousel slide wt-keycontacts" data-ride="carousel">  
  2.     <span>Key Contacts</span>  
  3.     <div id="Key_Contacts" class="carousel-inner">  
  4.   
  5.     </div>  
  6.     <!-- Carousel controls -->  
  7.     <a class="carousel-control wt-left-carousel-control" href="#KeyContactsContainer" data-slide="prev">  
  8.         <span class="glyphicon glyphicon-chevron-left"></span>  
  9.     </a>  
  10.     <a class="carousel-control wt-right-carousel-control" href="#KeyContactsContainer" data-slide="next">  
  11.         <span class="glyphicon glyphicon-chevron-right"></span>  
  12.     </a>  
  13. </div>  
  14. <script type="text/javascript" language="javascript">  
  15.     $(document).ready(function () {  
  16.         var i = 0;  
  17.         var ListUrl = _spPageContextInfo.siteServerRelativeUrl + "/_api/web/lists/getbytitle('Key Contacts')/items";  
  18.         var Select = "?$select=Contact/FirstName,Contact/LastName,Contact/JobTitle,Contact/WorkPhone,Contact/EMail,Contact/UserName&$expand=Contact/Id";  
  19.         var Sort = "&$orderby=Position asc";  
  20.         var Filter = "&$filter=ContentType eq 'Key Contact'";  
  21.         var Query = Select + Sort + Filter;  
  22.         $.ajax({  
  23.             url: ListUrl + Query,  
  24.             method: "GET",  
  25.             headers: { "Accept""application/json; odata=verbose" },  
  26.             success: function (data) {  
  27.                 if (data.d != undefined) {  
  28.                     var KeyContactsHTML = "";  
  29.   
  30.                     $.each(data.d.results, function (index, item) {  
  31.   
  32.                         $("#KeyContactsContainer").show();  
  33.                         var FirstName = item.Contact.FirstName;  
  34.                         var LastName = item.Contact.LastName;  
  35.                         var JobTitle = item.Contact.JobTitle || "";  
  36.                         var WorkPhone = item.Contact.WorkPhone || "";  
  37.                         var EMail = item.Contact.EMail;  
  38.                         var UserName = item.Contact.UserName;  
  39.                         UserName = UserName.split("@")[0];  
  40.                         var ImgSrc = _spPageContextInfo.siteServerRelativeUrl + "/_layouts/15/userphoto.aspx?size=L&accountname=" + EMail  
  41.   
  42.                         ImgSrc = ImgSrc.replace("UserName", UserName);  
  43.   
  44.                         KeyContactsHTML = "<div class='item" + (i == 0 ? " active" : "") + "'>"  
  45.                             + "<img src='" + ImgSrc + "' class='img-circle wt-keycontacts-image' style='min-height: 100px;'>"  
  46.                             + "<div class='wt-keycontacts-text'>"  
  47.                             + "<div class='wt-keycontacts-text-name'>" + FirstName + "  " + LastName + "</div>"  
  48.                             + "<div class='wt-keycontacts-text-title'>" + JobTitle + "</div>"  
  49.                             + "<div class='wt-email'>" + EMail + "</div>"  
  50.                             + "<div class='wt-phone' style='padding-left: 20px;'>" + WorkPhone + "</div>"  
  51.                             + "</div ></div > ";  
  52.   
  53.                         $("#Key_Contacts").append(KeyContactsHTML);  
  54.   
  55.                         //increase count until .each is done.  
  56.                         i++;  
  57.                     });  
  58.   
  59.                 }  
  60.             },  
  61.             error: function (xhr, ajaxOptions, thrownError) {  
  62.                 alert("POST error:\n" + xhr.status + "\n" + thrownError);  
  63.             }  
  64.         });  
  65.   
  66.     });  
  67.   
  68. </script> 

Up Next
    Ebook Download
    View all
    Learn
    View all