7

As seen in the following picture, when I add a User-Filter to a page it allows me to choose the parameter to pass. In my case I used the Büro.

Enter image description here

How can I retrive this data from client using JavaScript? I already tried with the following code, but the fields I need are not in this object.

var context = SP.ClientContext.get_current()
var web = context.get_web();
var user = web.get_currentUser();
context.load(user);
context.executeQueryAsync()

2 Answers 2

8

Getting only the current user won't be enough to get user profile properties. You need to use the PeopleManager as well. Your code should look something like this:

var context = SP.ClientContext.get_current()
var web = context.get_web();
var user = web.get_currentUser();
var peopleManager = new SP.UserProfiles.PeopleManager(context);
var personProperties = peopleManager.getPropertiesFor(user.get_loginName());
context.load(personProperties);
context.executeQueryAsync(onRequestSuccess, onRequestFail);

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

function onRequestFail(sender, args) {
    $get("results").innerHTML = "Error: " + args.get_message();
}

Since I don't have access to DEV environment now, I can't tell you for sure, but you might need to add domain name in front of user.get_loginName() on the following line of code. As far as I remember it should be DOMAIN\USERNAME:

var personProperties = peopleManager.getPropertiesFor(user.get_loginName());

Hope this helps.

2
  • If you found it helpful, mark it as an answer. Thanks :) Jun 26, 2015 at 10:28
  • Of course...the system says i have to wait 3 minutes :)
    – Emaborsa
    Jun 26, 2015 at 10:31
3

Since I have to load the userprofile script, I had to use:

SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', function() {

    var clientContext = new SP.ClientContext.get_current();
    var personProperties = new SP.UserProfiles.PeopleManager(clientContext).getMyProperties();
    clientContext.load(personProperties);
    clientContext.executeQueryAsync(function() {
            personProperties.get_userProfileProperties()['Office'];
        })
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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