Get Personal Information For a Specific User in SharePoint 2013 CSOM

Introduction

In the SharePoint 2013 environment, SP.UserProfiles JavaScript is used to get user profiles and user properties in custom solutions and apps for SharePoint 2013.

There are various objects supported in the SP.userProfile namespace. They are as follows:
  • HashTag
  • HashTagCollection
  • PeopleManager
  • PersonProperties
  • ProfileLoader
  • UserProfile
  • UserProfilePropertiesForUser

In this article, I will provide sample code to get the personal information for a specific target user.

The personal information that can be fetched in this article is:

  • AccountName
  • Person
  • Assistant
  • Person
  • CellPhone
  • Mobile phone
  • Department
  • Fax
  • FirstName
  • HomePhone
  • LastName
  • Office
  • Personal site

Code

// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.

SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');

 

var userProfileProperties;

 

function getUserProperties() {

 

    // get the target users domain name and account name.

    var targetUser = "SomeDomain\\SomeUserName";

 

    // Get the current client context.

    var clientContext = new SP.ClientContext.get_current();

    //Get PeopleManager Instance

    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

 

    // Get the properties to retrieve

    var profilePropertyNames = ["AccountName", "FirstName",”"LastName"

    “Assistant", "Office","Personal site”

    “HomePhone", "Mobilephone",”CellPhone”,

    “Fax”,”Department” ];

    var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(

    clientContext,

    targetUser,

    profilePropertyNames);

 

    // Get user profile properties for the target user

    userProfileProperties =peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);

    // Load the UserProfilePropertiesForUser object.

    clientContext.load(userProfilePropertiesForUser)

    //Execute the Query

    clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);

}

 

// This function runs if the executeQueryAsync call succeeds.
 

function onRequestSuccess() {

    var messageText = "\" AccountName \" property is "

    + userProfileProperties[0];

    messageText += "<br />\" FirstName \" property is "

    + userProfileProperties[1];

    messageText += "<br />\" LastName \" property is "

    + userProfileProperties[2];

    messageText += "<br />\" Assistant \" property is "

    + userProfileProperties[3];

    messageText += "<br />\" Office \" property is "

    + userProfileProperties[4];

    messageText += "<br />\" Personal site \" property is "

    + userProfileProperties[5];

    messageText += "<br />\" HomePhone \" property is "

    + userProfileProperties[6];

    messageText += "<br />\" Mobilephone \" property is "

    + userProfileProperties[7];

    messageText += "<br />\" CellPhone \" property is "

    + userProfileProperties[8];

    messageText += "<br />\" Fax \" property is "

    + userProfileProperties[9];

    messageText += "<br />\" Department \" property is "

    + userProfileProperties[10];

 

    $get("Display").innerHTML = messageText;

}

 

// This function runs if the executeQueryAsync call fails.
 

function onRequestFail(sender, args) {

    $get("Display").innerHTML = "Error: " + args.get_message();

}

Code Walkthrough

  • UserProfilePropertiesForUser Method: Represents a set of user profile properties for a specified user.
  • getUserProfilePropertiesFor Method: Gets the specified user profile properties for the specified user.
  • onRequestSuccess: This will be called if the query is successful.
  • onRequestFail: This block will be executed if the query fails.
  • userProfileProperties: This variable holds the Manager Information.

Summary

I hope this article helps you in some aspects. Happy SharePointing.