How to: Retrieve user profile properties by using the JavaScript object model in SharePoint 2013

SharePoint 2013

Learn how to retrieve user properties and user profile properties programmatically by using the SharePoint 2013 JavaScript object model.

User properties and user profile properties provide information about SharePoint users, such as display name, email, title, and other business and personal information. In client-side APIs, you access these properties from the PersonProperties object and its userProfileProperties property. The userProfileProperties property contains all user profile properties, but the PersonProperties object contains commonly used properties (such as accountName, displayName, and email) that are easier to access.

The PeopleManager object includes the following methods that you can use to retrieve user properties and user profile properties by using the JavaScript object model:

User profile properties from client APIs are read-only (except the profile picture, which you can change by using the PeopleManager.setMyProfilePicture method). If you want to change other user profile properties, you must use the server object model. For more information about working with user profiles, see Work with user profiles in SharePoint 2013.

Note Note

The client-side UserProfile object doesn't contain all of the user properties as the server-side version. However, the client-side version does provide the methods for creating a personal site for the current user. To retrieve it, use the ProfileLoader.getUserProfile method.

To create an application page that uses the JavaScript object model to retrieve user properties, you'll need:

  • SharePoint Server 2013 with profiles created for the current user and a target user

  • Visual Studio 2012

  • Office Developer Tools for Visual Studio 2013

  • Full Control connection permissions to access the User Profile service application for the current user

  1. On the server running SharePoint Server 2013, open Visual Studio and choose File, New, Project.

  2. In the New Project dialog box, choose .NET Framework 4.5 from the drop-down list at the top of the dialog box.

  3. In the Templates list, expand Office/SharePoint, choose the SharePoint Solutions category, and then choose the SharePoint 2013 Project template.

  4. Name the project UserProfilesJSOM, and then choose the OK button.

  5. In the SharePoint Customization Wizard dialog box, enter the URL to your target SharePoint site, choose Deploy as a farm solution, and then choose the Finish button.

  6. In Solution Explorer, open the shortcut menu for the UserProfilesJSOM project, and then add a SharePoint "Layouts" mapped folder.

  7. In the Layouts folder, open the shortcut menu for the UserProfilesJSOM folder, and then add a new SharePoint application page named UserProfiles.aspx.

    Note Note

    The code examples in this article define custom code in the page markup but do not use the code-behind class file that Visual Studio creates for the page.

  8. Open the shortcut menu for the UserProfiles.aspx page, and then choose Set as Startup Item.

  9. In the markup for the UserProfiles.aspx page, paste the following code inside the "Main" asp:Content tags. This code adds a span control that displays the results of the query, SharePoint:ScriptLink controls that reference SharePoint JavaScript class library files, and script tags to contain your custom logic.

    <span id="results"></span><br />
    <SharePoint:ScriptLink ID="ScriptLink1" name="SP.js" runat="server"
        ondemand="false" localizable="false" loadafterui="true" />
    <SharePoint:ScriptLink ID="ScriptLink2" name="SP.UserProfiles.js" runat="server"
        ondemand="false" localizable="false" loadafterui="true" />
    <script type="text/javascript">
    
        // Replace this comment with the code for your scenario.
    
    </script>
    
    
  10. To add logic to retrieve user profile properties, replace the comment between the script tags with the code example from one of the following scenarios:

  11. To test the application page, on the menu bar, choose Debug, Start Debugging. If you’re prompted to modify the web.config file, choose the OK button.

The following code example shows how to get user profile properties for a target user by querying the PersonProperties object and its userProfileProperties property. It shows how to:

Note Note

Paste the following code between the script tags that you added to the UserProfiles.aspx file in the Create the application page procedure. Replace the domainName\\userName placeholder value before you run the code. (This code example does not use the code-behind class file.)

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();
}

The following code example retrieves the values for a specified set of user profile properties for a target user by using the getUserProfilePropertiesFor method. It shows how to:

Note Note

Paste the following code between the script tags that you added to the UserProfiles.aspx file in the Create the application page procedure. Replace the domainName\\userName placeholder value before you run the code. (This code example does not use the code-behind class file.)

var userProfileProperties;

// 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);

    // Specify the properties to retrieve and target user for the 
    // UserProfilePropertiesForUser object.
    var profilePropertyNames = ["PreferredName", "Department"];
    var userProfilePropertiesForUser = 
        new SP.UserProfiles.UserProfilePropertiesForUser(
            clientContext,
            targetUser,
            profilePropertyNames);

    // Get user profile properties for the target user.
    // To get the value for only one user profile property, use the
    // getUserProfilePropertyFor method.
    userProfileProperties = peopleManager.getUserProfilePropertiesFor(
        userProfilePropertiesForUser);

    // Load the UserProfilePropertiesForUser object and send the request.
    clientContext.load(userProfilePropertiesForUser);
    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("results").innerHTML = messageText;
}

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