SharePoint 2013: Working with User Profiles using CSOM

As we all know SharePoint 2013 provides strong support to client api. In this post we will talk about access user profile properties using client api. So now in SharePoint 2013 we can directly query to user profile and get all the user profile properties what we required.


  • References which we need to provide
    • <script src="/_layouts/15/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>
1. Get User Profile Properties for Current User
   
   $(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(){
  
    //Get Current Context 
    var clientContext = new SP.ClientContext.get_current();
    
    //Get Instance of People Manager Class
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    
    //Get properties of the current user
    userProfileProperties = peopleManager.getMyProperties();    
    clientContext.load(userProfileProperties);
    
    //Execute the Query.
    clientContext.executeQueryAsync(onSuccess, onFail);
  
  }
    
  function onSuccess() {
    console.log(userProfileProperties);
  }
  
  function onFail(sender, args) {
    alert("Error: " + args.get_message());
  } 

2. Get Single User Profile Property

    $(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 userProfileProperty;
  
  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 property = "PreferredName";
    
    //Domain\Username of the user (If you are on SharePoint Online) 
    var targetUser = "i:0#.f|membership|himanshu@yoursite.onmicrosoft.com";
    
    //If you are on On-Premise:
    //var targetUser = domain\\username
    
    //Create new instance of UserProfileProperty
    userProfileProperty = peopleManager.getUserProfilePropertyFor(targetUser, propertyName)
    
    //Execute the Query. (No load method necessary)
    clientContext.executeQueryAsync(onSuccess, onFail);
  
  }
    
  function onSuccess() {  
    var messageText = "\"Preferred Name\" property is " + userProfileProperty.get_value();
    alert(messageText);  
  }
  
  function onFail(sender, args) {
    alert("Error: " + args.get_message());
  } 

3. Get Multiple User Profile Properties

   $(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(){  
    //Get Current Context 
    var clientContext = new SP.ClientContext.get_current();
    
    //Get Instance of People Manager Class
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    
    //Properties to fetch from the User Profile
    var profilePropertyNames = ["PreferredName","PictureURL"];
    
    //Domain\Username of the user (If you are on SharePoint Online) 
    var targetUser = "i:0#.f|membership|himanshu@yoursite.onmicrosoft.com"; 
    
    //If you are on On-Premise:
    //var targetUser = domain\\username
    
    //Create new instance of UserProfilePropertiesForUser
    var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, targetUser, profilePropertyNames);
    userProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
    
    //Execute the Query.
    clientContext.load(userProfilePropertiesForUser);
    clientContext.executeQueryAsync(onSuccess, onFail);
    
  }
    
  function onSuccess() {    
    var messageText = "\"Preffered Name\" property is " + userProfileProperties[0];
    messageText += "\"PictureURL\" property is " + userProfileProperties[1];    
    alert(messageText);      
  }
 
  function onFail(sender, args) {
    alert("Error: " + args.get_message());
  } 

4. Get Multiple Users Profile Properties

    $(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 usersnames any way you want.
  var targerUsers = ["i:0#.f|membership|himanshu@yoursite.onmicrosoft.com","i:0#.f|membership|user1@yoursite.onmicrosoft.com"];
  
  //If you are on On-Premise:
  //var targerUsers = ["domain\\username","domain\\user1"];
  
  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(onSuccess, onFail);
  
  }
  
  function onSuccess() {
    var messageText = "";
  
    for(var i=0;i<userProfileProperties.length;i++){
      messageText += "\"Preffered Name\" property is " + userProfileProperties[i].get_value();
    }
    alert(messageText);
  }
  
  function onFail(sender, args) {
    alert("Error: " + args.get_message());
  } 

Comments

Popular posts from this blog

C# Copy files from one server to another

Export SharePoint Online Group Users to Excel using Powershell