Get User Properties in SharePoint Online

This article explains how to get the user properties in SharePoint Online.

Now you can directly query the user profiles and get the required data from the client side.

In SharePoint Online we can only deploy the sandboxed solutions. There are some restrictions in using Microsoft.Office.Server.UserProfiles in SharePoint sandboxed solutions. If you want to get properties from user profiles then you can use the script shown below.

Step 1: In the SharePoint sandboxed web part (.ascx page) add the following references as shown below.

(jQuery is not required but I have added it because we will need it for the $.ajax function when doing REST queries.)

  1. <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.0.min.js"></script>  
  2. <script src="/_layouts/15/SP.Runtime.js"></script>  
  3. <script src="/_layouts/15/SP.js"></script>  
  4. <script src="/_layouts/15/SP.UserProfiles.js"></script>  
Get properties of current user from user profile

Here is the script segment to get the current user profile properties. Add the following script in your web part “.ascx” page:
  1. <script type="text/javascript">  
  2. (function ($) {  
  3. //debugger;  
  4. $(document).ready(function () {  
  5. // ensure that the SP.UserProfiles.js file is loaded before the custom code runs.  
  6. SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');  
  7. });  
  8. var userProfileProperties;  
  9. function loadUserData()   
  10. {  
  11. //debugger;  
  12. //Get Current Context    
  13. var clientContext = new SP.ClientContext.get_current();  
  14. //Get Instance of People Manager Class  
  15. var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);  
  16. userProfileProperties = peopleManager.getMyProperties();  
  17. clientContext.load(userProfileProperties);  
  18. clientContext.executeQueryAsync(onSuccessful, onFailure);  
  19. }  
  20. function onSuccessful (sender, args) {  
  21. //debugger;  
  22. //Get default properties  
  23. var username = userProfileProperties.get_displayName();  
  24. var desigination = userProfileProperties.get_title();  
  25. var pictureURL = userProfileProperties.get_pictureUrl();  
  26. //Get custom properties  
  27. var employeeID = userProfileProperties.get_userProfileProperties().EmployeeID;  
  28. // alert(username + "/" + pictureURL + "/"  + employeeID + "/" + desigination);  
  29.    
  30. // lblEmployeeDesignationValue, lblEmployeeIdValue – lable id  
  31. document.getElementById('<%= lblEmployeeDesignationValue.ClientID %>').innerHTML = desigination;  
  32. document.getElementById('<%= lblEmployeeIdValue.ClientID %>').innerHTML = employeeID;  
  33. if (pictureURL != null || pictureURL == "" ) {  
  34. document.getElementById('<%= imgUser.ClientID %>').src = pictureURL;  
  35. }  
  36. else {  
  37. document.getElementById('<%= imgUser.ClientID %>').src =  
  38. “/_catalogs/masterpage/MetroOnlineLMSStyles/images/UserImage.jpg"  
  39.   }  
  40. }  
  41. function onFailure(sender, args)   
  42. {  
  43. alert("Error: " + args.get_message());  
  44. }  
  45. </script>  

 

That's all.

Get Properties of Multiple Users in Single Request

Here is the code segment to get the multiple user profile properties in a single request. Add the following script in your web part “ASCX” page.

We can use the following script for SharePoint on-premises and online (o365) also.

  1. <script type="text/JavaScript">  
  2. (function($)  
  3.  {  
  4.   $(document).ready(function()  
  5. {  
  6.     // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.  
  7.     SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');    
  8.   });  
  9.   var userProfileProperties = [];  
  10.     
  11.   //Array containing domain\usernames of multiple users. You can get the usernames any way you want.  
  12.  var targerUsers = ["i:0#.f|membership|[email protected]","i:0#.f|membership|[email protected]"];  
  13.  //If you are on On-Premise:  
  14.  //var targerUsers = ["domain\\username","domain\\demouser1"];  
  15.   function loadUserData(){  
  16.      //Get Current Context            
  17.     var clientContext = new SP.ClientContext.get_current();  
  18.   
  19.     //Get Instance of People Manager Class  
  20.     var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);  
  21.     //Property to fetch from the User Profile  
  22.     var propertyName = "PreferredName";              
  23.     for(var i=0;i<targerUsers.length;i++){  
  24.       //Create new instance of UserProfileProperty  
  25.       userProfileProperties[i] = peopleManager.getUserProfilePropertyFor(targerUsers[i], propertyName);  
  26.     }  
  27.     //Execute the Query. (No load method necessary)  
  28.    clientContext.executeQueryAsync(onSuccessful, onFailure);  
  29.    }  
  30.   //Success method  
  31.   function onSuccessful () {  
  32.     var message = "";    
  33.     for(var i=0;i<userProfileProperties.length;i++){  
  34.       message += "\" Name\" property is " + userProfileProperties[i].get_value();  
  35.     }  
  36.     alert(message);  
  37.   }  
  38.   //Failure method  
  39.   function onFailure (sender, args) {  
  40.     alert("Error: " + args.get_message());  
  41.   }           
  42. })(jQuery);  
  43. </script>  

 

That's all. The code above gets the multiple user properties in a single request.

Summary

This article explored getting the user properties from user profiles in SharePoint online.

Up Next
    Ebook Download
    View all
    Learn
    View all