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.)
- <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.0.min.js"></script>
- <script src="/_layouts/15/SP.Runtime.js"></script>
- <script src="/_layouts/15/SP.js"></script>
- <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:
- <script type="text/javascript">
- (function ($) {
-
- $(document).ready(function () {
-
- SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');
- });
- var userProfileProperties;
- function loadUserData()
- {
-
-
- var clientContext = new SP.ClientContext.get_current();
-
- var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
- userProfileProperties = peopleManager.getMyProperties();
- clientContext.load(userProfileProperties);
- clientContext.executeQueryAsync(onSuccessful, onFailure);
- }
- function onSuccessful (sender, args) {
-
-
- var username = userProfileProperties.get_displayName();
- var desigination = userProfileProperties.get_title();
- var pictureURL = userProfileProperties.get_pictureUrl();
-
- var employeeID = userProfileProperties.get_userProfileProperties().EmployeeID;
-
-
-
- document.getElementById('<%= lblEmployeeDesignationValue.ClientID %>').innerHTML = desigination;
- document.getElementById('<%= lblEmployeeIdValue.ClientID %>').innerHTML = employeeID;
- if (pictureURL != null || pictureURL == "" ) {
- document.getElementById('<%= imgUser.ClientID %>').src = pictureURL;
- }
- else {
- document.getElementById('<%= imgUser.ClientID %>').src =
- “/_catalogs/masterpage/MetroOnlineLMSStyles/images/UserImage.jpg"
- }
- }
- function onFailure(sender, args)
- {
- alert("Error: " + args.get_message());
- }
- </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.
- <script type="text/JavaScript">
- (function($)
- {
- $(document).ready(function()
- {
-
- SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');
- });
- var userProfileProperties = [];
-
-
- var targerUsers = ["i:0#.f|membership|[email protected]","i:0#.f|membership|[email protected]"];
-
-
- function loadUserData(){
-
- var clientContext = new SP.ClientContext.get_current();
-
-
- var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
-
- var propertyName = "PreferredName";
- for(var i=0;i<targerUsers.length;i++){
-
- userProfileProperties[i] = peopleManager.getUserProfilePropertyFor(targerUsers[i], propertyName);
- }
-
- clientContext.executeQueryAsync(onSuccessful, onFailure);
- }
-
- function onSuccessful () {
- var message = "";
- for(var i=0;i<userProfileProperties.length;i++){
- message += "\" Name\" property is " + userProfileProperties[i].get_value();
- }
- alert(message);
- }
-
- function onFailure (sender, args) {
- alert("Error: " + args.get_message());
- }
- })(jQuery);
- </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.