Latest Posts

SharePoint REST API - Get user profile properties

In this article, we are going to see how we can fetch User Profile properties of logged in user as well as for any other user using a REST API call. This code will work both in SharePoint Online as well as in SharePoint on-premises.

Prerequisite:

  • First, you need to add a JQuery reference on your page.
  • Then you can put below code in either script editor web part or in JS File and refer that JS file on your page.

Get Current user profile properties using REST API

  • In this section, we will see how to get the profile properties for the logged-in user itself.
  • The REST-API endpoint is - /_api/SP.UserProfiles.PeopleManager/GetMyProperties
  • Please find below the complete method.
  • If you want to get current user profile picture (which is also covered in example below), you can use - _spPageContextInfo.webAbsoluteUrl + '/_layouts/15/userphoto.aspx?size=S&accountname=' + <<userEmail>>

Note:

  • You will be able to access all the default profile properties using below method but If you are not able to access custom properties, then refer the method (GetUserPropertiesUsingAccountName) mentioned in the next section of the article.
  • $(document).ready(function () {
  •    getUserProfileProperties();
  • });
  • function getUserProfileProperties() {
  • $.ajax({
  •         url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties",
  •         method: "GET",
  •         headers: { "Accept": "application/json; odata=verbose" },
  •         async: true,
  •         cache: true,
  •         success: function (data) {
  •             // Display Name of the logged-in user
  •             var name = data.d.DisplayName;
  •             var userUrl = data.d.UserUrl;
  •  
  •             //Email address of the logged-in user
  •             var userEmail = data.d.Email;
  •             var PersonalUrl = data.d.PersonalUrl;
  •  
  •             // You can get the picture URL of the logged-in user as well.
  •             // You just need to specify the account name and Size of the photo here.
  •             var pictureUrl = _spPageContextInfo.webAbsoluteUrl + '/_layouts/15/userphoto.aspx?size=S&accountname=' + userEmail;
  •         },
  •         error: function (errorThrown) {
  •             // Log errors in browsers console if any
  •             console.info('Ajax Request failed in getUserProfileProperties function :' + errorThrown.statusText + ' || ' + errorThrown.responseText);
  •         }
  •     });
  • }

Get User Profile Properties using REST API

  • If you are accessing Person and Group field from a SharePoint list using REST APIs, then you will get user Id while fetching the data and using User Id you will NOT be able to fetch user profile properties directly.
  • We will show you in below code how to can get the User Profile Properties using User Id and also using User Account Name.

Note:

  • If you want to get user profile properties by User account name, then you can directly call the method – GetUserPropertiesUsingAccountName. This method will take the endpoint URL as a parameter
  • But if you want to get user profile properties by User Id, then refer method – GetUserPropertiesById which gets the User Account name using Id and then gets all the properties.
  • IMPORTANT – You can notice that the GetUserPropertiesUsingAccountName method gets property directly from User Profile Service while the GetUserPropertiesById method gets user properties using REST call on the site.
  • $(document).ready(function () {
  •     // Enter the login name of the user whose property you want to see
  •     var loginName = "i:0#.f|membership|[email protected]";
  •     var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/sp.userprofiles.peoplemanager/" +
  •         "GetPropertiesFor(AccountName=@v)?@v='" + encodeURIComponent(loginName) + "'";
  •     GetUserPropertiesUsingAccountName(requestUrl);
  • });
  •  
  • // Function to search people details
  • function GetUserPropertiesUsingAccountName(requestUrl) {
  •     var restConstants = {
  •         ACCEPT: "application/json;odata=verbose",
  •         CONTENTTYPE: "application/json;odata=verbose"
  •     };
  •  
  •     var requestHeaders = {
  •         "Accept": restConstants.ACCEPT,
  •         "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
  •     };
  •     jQuery.ajax({
  •         type: "POST",
  •         url: requestUrl,
  •         async: false,
  •         contentType: restConstants.CONTENTTYPE,
  •         headers: requestHeaders,
  •         beforeSend: function (x) {
  •             if (x && x.overrideMimeType) {
  •                 x.overrideMimeType("application/j-son;charset=UTF-8");
  •             }
  •         },
  •         dataType: "json",
  •         success: function (data) {           
  •             if (data.d.UserProfileProperties != null && data.d.UserProfileProperties != 'undefined') {
  •                 var resultsObject = data.d.UserProfileProperties.results;
  •                 var newCollection = null;
  •                 var LocationDescription = '';
  •                 var WorkEmail = '';
  •  
  •                 //LocationDescription                        
  •                 newCollection = resultsObject.filter(function (result) { return result.Key == "LocationDescription" });
  •                 jQuery.each(newCollection, function (index, result) {
  •                     LocationDescription = result.Value;
  •                 });
  •                 // LocationDescription
  •  
  •                 // Work Email
  •                 newCollection = resultsObject.filter(function (result) { return result.Key == "WorkEmail" });
  •                 jQuery.each(newCollection, function (index, result) {
  •                     WorkEmail = result.Value;
  •                 });
  •                 // Work Email
  •             }
  •         },
  •         error: function (jqxr, errorCode, errorThrown) {
  •         }
  •     });
  • }
  • //// This method gets user properties based on User Id. ////
  • function GetUserPropertiesById() {
  •     // Provide the user ID. You can get the user-id from a hidden list called - User Information List
  •     var UserId = 10;
  •  
  •     var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + UserId + ")";
  •     var requestHeaders = { "accept": "application/json;odata=verbose" };
  •  
  •     $.ajax({
  •         //type: "GET",
  •         url: requestUri,
  •         async: false,
  •         contentType: "application/json;odata=verbose",
  •         headers: requestHeaders,
  •         success: function (data) {
  •             var loginName = data.d.LoginName;
  •  
  •            // If you want to access User Profile Service to get more properties then you can execute below line which will call the above method to "get properties using the account name".
  •             var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/sp.userprofiles.peoplemanager/" +
  •                 "GetPropertiesFor(AccountName=@v)?@v='" + encodeURIComponent(loginName) + "'";
  •             GetUserPropertiesUsingAccountName(requestUrl);
  •         },
  •         error: function (errorThrown) {
  •             // Log errors in browsers console if any
  •             console.info('Ajax Request failed in GetUserProperties function : ' + errorThrown.statusText + ' || ' + errorThrown.responseText);
  •         }
  •     });
  • }

Please let us know in the comment section if anything is missing or you want to add something related to User Profile Properties.


We value your Feedback:

Page URL:

Name:

Email:


Suggestion:

© 2024 Code SharePoint