Get Manager Information For a Specific User in SharePoint 2013 CSOM

 Introduction

In a 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 given as below.

  • HashTag
  • HashTagCollection
  • PeopleManager
  • PersonProperties
  • ProfileLoader
  • UserProfile
  • UserProfilePropertiesForUser

In this article, I will provide the sample code to get the manager information for a specific target user. I guess everyone will be interested in knowing your manager name for specific good reasons.

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 = ["PreferredName", "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 = "\"PreferredName\" property is "

    + userProfileProperties[0];

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

    + userProfileProperties[1];

    $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 becomes successful
onRequestFail: This block will be execute if the query fails.
userProfileProperties: This variable holds the Manager Information.

Summary

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

Up Next
    Ebook Download
    View all
    Learn
    View all