2

I've seen how to get information about the current user using CSOM/JSOM from Get current user in Client Object Model with javascript? but I can't figure out how to get other properties like:

  • department
  • title
  • manager
  • phone number

I know how to do it using /_vti_bin/Lists.asmx on the UserInfo table but I was wanted to try using CSOM/JSOM instead.

3

You should use SP.UserProfiles.js to get user profile properties. Example:

var personProperties;

// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');

function getUserProperties() {

    // Replace the placeholder value with the target user's credentials.
    var targetUser = "domainName\\userName";

    // Get the current client context and PeopleManager instance.
    var clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

    // Get user properties for the target user.
    // To get the PersonProperties object for the current user, use the
    // getMyProperties method.
    personProperties = peopleManager.getPropertiesFor(targetUser);

    // Load the PersonProperties object and send the request.
    clientContext.load(personProperties);
    clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}

// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {

    // Get a property directly from the PersonProperties object.
    var messageText = " \"DisplayName\" property is "
        + personProperties.get_displayName();

    // Get a property from the UserProfileProperties property.
    messageText += "<br />\"Department\" property is "
        + personProperties.get_userProfileProperties()['Department'];
    $get("results").innerHTML = messageText;
}

// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
    $get("results").innerHTML = "Error: " + args.get_message();
}

See this for more information: https://msdn.microsoft.com/en-us/library/office/jj920104.aspx

  • Oh wow. Thank you so much @Nadeem. That is fantastic! – IMTheNachoMan Nov 19 '15 at 14:06
  • This will only work for users who either have access to the site or have ever visited it. Correct? Anyway to get it for all users, even if they don't have access and have never been to the site? – IMTheNachoMan Dec 14 '15 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.