Introduction
In this article, we will learn how to fetch the user details and profile URL using REST API.
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
- 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.
For carousel (scroll) functionality, insert bootstrap CSS reference. Since I added it globally in the master page, I have not included that here.
- <div id="KeyContactsContainer" class="carousel slide wt-keycontacts" data-ride="carousel">
- <span>Key Contacts</span>
- <div id="Key_Contacts" class="carousel-inner">
- </div>
- <!-- Carousel controls -->
- <a class="carousel-control wt-left-carousel-control" href="#KeyContactsContainer" data-slide="prev">
- <span class="glyphicon glyphicon-chevron-left"></span>
- </a>
- <a class="carousel-control wt-right-carousel-control" href="#KeyContactsContainer" data-slide="next">
- <span class="glyphicon glyphicon-chevron-right"></span>
- </a>
- </div>
- <script type="text/javascript" language="javascript">
- $(document).ready(function () {
- var i = 0;
- var ListUrl = _spPageContextInfo.siteServerRelativeUrl + "/_api/web/lists/getbytitle('Key Contacts')/items";
- var Select = "?$select=Contact/FirstName,Contact/LastName,Contact/JobTitle,Contact/WorkPhone,Contact/EMail,Contact/UserName&$expand=Contact/Id";
- var Sort = "&$orderby=Position asc";
- var Filter = "&$filter=ContentType eq 'Key Contact'";
- var Query = Select + Sort + Filter;
- $.ajax({
- url: ListUrl + Query,
- method: "GET",
- headers: { "Accept": "application/json; odata=verbose" },
- success: function (data) {
- if (data.d != undefined) {
- var KeyContactsHTML = "";
- $.each(data.d.results, function (index, item) {
- $("#KeyContactsContainer").show();
- var FirstName = item.Contact.FirstName;
- var LastName = item.Contact.LastName;
- var JobTitle = item.Contact.JobTitle || "";
- var WorkPhone = item.Contact.WorkPhone || "";
- var EMail = item.Contact.EMail;
- var UserName = item.Contact.UserName;
- UserName = UserName.split("@")[0];
- var ImgSrc = _spPageContextInfo.siteServerRelativeUrl + "/_layouts/15/userphoto.aspx?size=L&accountname=" + EMail
- ImgSrc = ImgSrc.replace("UserName", UserName);
- KeyContactsHTML = "<div class='item" + (i == 0 ? " active" : "") + "'>"
- + "<img src='" + ImgSrc + "' class='img-circle wt-keycontacts-image' style='min-height: 100px;'>"
- + "<div class='wt-keycontacts-text'>"
- + "<div class='wt-keycontacts-text-name'>" + FirstName + " " + LastName + "</div>"
- + "<div class='wt-keycontacts-text-title'>" + JobTitle + "</div>"
- + "<div class='wt-email'>" + EMail + "</div>"
- + "<div class='wt-phone' style='padding-left: 20px;'>" + WorkPhone + "</div>"
- + "</div ></div > ";
- $("#Key_Contacts").append(KeyContactsHTML);
- //increase count until .each is done.
- i++;
- });
- }
- },
- error: function (xhr, ajaxOptions, thrownError) {
- alert("POST error:\n" + xhr.status + "\n" + thrownError);
- }
- });
- });
- </script>

Tanner HolmPosted Jun 9, 2020, 3:49 PM
Super helpful, thank you!
Majid malikPosted Nov 2, 2018, 8:31 AM
Sir Sandeep -- As I understand 'NameWithPictureAndDetails' lookup value has to be computed using other field values such as Name field value and Picture URL value (I got this url value from managed client ListItem coming out of CamlQuery on UserInfoList). Or please tell me if there is a place where this lookup field is sitting with already computed values. I want to show this field value to user in a desktop application. Also please where is picture size available from? And also how would somebody update this field for any List using 'Person And Group' type of field? I am using Micrsoft.Sharepoint.Client object model.
Paje everPosted Jul 2, 2018, 10:16 AM
Hi Sandeep, Using SP 2013, is there a way to edit the default "People and Groups - Name (with picture and details)" code? I have a list of users and their default images are most all blank; however, I have a portraits photo library where I could link each user to an image. (I'd like to do this without editing their main user profile from the /admin site.)
Rajesh YPosted Jun 11, 2018, 8:19 AM
Can you please share css for carousel.
Amish rajPosted Mar 14, 2018, 11:24 AM
Hi ,I tried to retrieve the WorkPhone using the same rest api but it was giving error"XYZ/WorkPhone is not valid" but when i tried to retrieve MobilePhone and email id its gives me the output.please guide me where i am going wrong
Mahesh BabuPosted Sep 22, 2017, 2:39 AM
Good information Sandeep.. Thank you.