Selecting User Profile values from the people picker fields

Document created by AdisJ Employee on Aug 17, 2017Last modified by Matthias_Walter on Jan 10, 2018
Version 2Show Document
  • View in full screen mode

Very often there is a need to select the User Profile properties for another user apart of "my user" (the user that is currently logged in). Since this is not implemented in skybow Solution Studio and skybow Rich Forms out of the box, due to the issue with permissions and security (the user who is querying the user profile information needs to have appropriate permissions to actually do it), here is a workaround how to implement this manually.

In the use case below, there are two fields on the form: a People Picker field bound to the field with internal name "PersonField", and a Text field, bound to the field with internal name "DepartmentField". The goal is, that when a person is selected in the people picker field ("PersonField"), that the "DepartnemtField" is automatically populated with the Department value from that user's profile.

 

In the "Behavior" ribbon group of the "DepartmentField", in the "Calculated" value, the following "Function code" script should be set.

 

Please note, that in lines 9, 45 and 56 proper field names should be set for people picker and text field, and in the line 43 proper user profile property name. 

Important: since this script will always be executed in the user context, if logged user does not have rights to read User Profile property values, the script will not function for that user.

 

var personProperties;

if (typeof SP.UserProfiles == 'undefined') {
    var scriptKey = 'SP.UserProfiles.js';
    SP.SOD.registerSod(scriptKey, '/_layouts/15/SP.UserProfiles.js');
    SP.SOD.loadMultiple(['SP.UserProfiles.js'], function(){});
}

var targetUser = [[PersonField.LoginName]];
if(!targetUser)
{
    return '';
}

if(!window.ArdUserInProgress)
{
    // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
    SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');
    window.ArdUserInProgress = true;
}

function getUserProperties() {
 
    // 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 from the UserProfileProperties property.
    var messageText = personProperties.get_userProfileProperties()["Department"];
 
    var inputController = ArdeviaJQuery('span.ms-formbody[fieldname="DepartmentField"]').closest('.ard-formfield').data('Controller');
    inputController.InstanseData.SetValue(messageText);
    window.ArdUserInProgress = false;
}

// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
    window.ArdUserInProgress = false;
    alert("Error: " + args.get_message());
}

return [[DepartmentField]];

Attachments

    Outcomes