.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Get User Properties in SharePoint Online

Posted By:Kaviya Balasubramanian       Posted Date: December 31, 2013    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

In this article, I'll explain a way 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 make use of 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 ($) {

//debugger;
$(document).ready(function () {
// ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');
});

var userProfileProperties;

function loadUserData() 
{
//debugger;
//Get Current Context
  
var clientContext = new SP.ClientContext.get_current();
//Get Instance of People Manager Class
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
userProfileProperties = peopleManager.getMyProperties();
clientContext.load(userProfileProperties);
clientContext.executeQueryAsync(onSuccessful, onFailure);
}

function onSuccessful (sender, args) {
//debugger;
//Get default properties
var username = userProfileProperties.get_displayName();
var desigination = userProfileProperties.get_title();
var pictureURL = userProfileProperties.get_pictureUrl();
//Get custom properties
var employeeID = userProfileProperties.get_userProfileProperties().EmployeeID;
// alert(username + "/" + pictureURL + "/"  + employeeID + "/" + desigination);
 
// lblEmployeeDesignationValue, lblEmployeeIdValue - lable id

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()
{
    // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
    SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');  
  });

  var userProfileProperties = [];
  
  //Array containing domain\usernames of multiple users. You can get the usernames any way you want.
 var targerUsers = "i:0#.f|membership|user@yoursite.onmicrosoft.com","i:0#.f|membership|user1@yoursite.onmicrosoft.com"];
 //If you are on On-Premise:
 //var targerUsers = ["domain\\username","domain\\demouser1"];

  function loadUserData(){

     //Get Current Context          
    var clientContext = new SP.ClientContext.get_current();

    //Get Instance of People Manager Class
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

    //Property to fetch from the User Profile
    var propertyName = "PreferredName";            

    for(var i=0;i<targerUsers.length;i++){

      //Create new instance of UserProfileProperty
      userProfileProperties[i] = peopleManager.getUserProfilePropertyFor(targerUsers[i], propertyName);
    }

    //Execute the Query. (No load method necessary)
   clientContext.executeQueryAsync(onSuccessful, onFailure);

   }

  //Success method

  function onSuccessful () {
    var message = "";  
    for(var i=0;i<userProfileProperties.length;i++){
      message += "\" Name\" property is " + userProfileProperties[i].get_value();
    }
    alert(message);
  }
  //Failure method
  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

In this article we have explored getting the user properties from user profiles in SharePoint online.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend