1

I'm trying to get a custom user profile property, but I only get that the property is undefined.

What I'm trying to do is to get a custom user profile property, without having to loop trough the arrays of properties.

ex from microsoft .. https://msdn.microsoft.com/en-us/library/office/jj920104.aspx

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 = "awesome\administrator";

    // 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 = ["PolicyAccept"];
    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 = "PolicyAccept property is "
        + userProfileProperties[0];
    $('#MiddleLeftCell').prepend("<h1>" +messageText +"</h1>");
}

// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
    $('#MiddleLeftCell').prepend("<h1>Error: " + args.get_message());
}

I've got this to work, but not without having to loop trough the whole array of properties. ex:

data.d.UserProfileProperties.results.filter(function (a, b) { return a.Key == name; })[0].Value;

Every property got a key so my other question is .. isn't there a way to have a direct lookup on a custom property?

Yes I've checked if the property is there and it's visible.

UPDATE / ANSWER

Found what was wrong! The encoding in targetuser. Remeember to have \ instead of \

var targetUser = "awesome\\administrator";
  • Please, clarify, if you would like to use REST (title of the question and 2nd code block) or client object model (main code block). In the client OM the syntax you use (I mean userProfileProperties[0]) should work. – pholpar Mar 16 '15 at 9:10
  • Found what was wrong! var targetUser = "awesome\administrator"; Should be var targetUser = "awesome\\administrator"; – Plexus81 Mar 16 '15 at 9:39
0

Found what was wrong! The encoding in targetuser. Remeember to have \ instead of \ . (two slashes instead of one)

var targetUser = "awesome\\administrator";

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.