3

How do I get the "User Information" for a specified user using JSLink?

With CSR, I can get some user details from ctx.CurrentItem.PersonField[0]. This gives me an array of users which are selected for that item. Each one looks like:

department: "HR"
email: "[email protected]"
id: "1024"
jobTitle: "Director of directing"
picture: "image.png"
sip: ""
title: "Firstname Lastname"

A few things from the full user profile are missing here (I care about "Mobile Number" and "About Me"). I'm guessing that's because they're not relevant to the normal view. The column is set to Name (with picture and details). The other options give less information, but all have the same issue.

My current and very ugly solution is to have two "Person or Group" fields: one that's set to "Name (with picture and details)" and one that's set to "Mobile Number" and read from both of them.

Is there a better way?

2 Answers 2

4

You can't get all user metadata(profile) data from people column. It has to be retrieved from User profile properties.

(function($){

  $(document).ready(function(){
    // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
    SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');  
  });

  var userProfileProperties = [];

  //Array containing domain\usernames of multiple users. You can get the usernames any way you want.
  var targerUsers = ["i:0#.f|membership|[email protected]","i:0#.f|membership|[email protected]"];

  //If you are on On-Premise:
  //var targerUsers = ["domain\\username","domain\\demouser1"];

  function loadUserData(){

    //Get Current Context   
    var clientContext = new SP.ClientContext.get_current();

    //Get Instance of People Manager Class
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

    //Property to fetch from the User Profile
    var propertyName = "WorkPhone";     

    for(var i=0;i<targerUsers.length;i++){

      //Create new instance of UserProfileProperty
      userProfileProperties[i] = peopleManager.getUserProfilePropertyFor(targerUsers[i], propertyName);
    }

    //Execute the Query. (No load method necessary)
    clientContext.executeQueryAsync(onSuccess, onFail);

  }

  function onSuccess() {
    var messageText = "";

    for(var i=0;i<userProfileProperties.length;i++){
      messageText += "\"WorkPhone\" property is " + userProfileProperties[i].get_value();
    }
    alert(messageText);
  }

  function onFail(sender, args) {
    alert("Error: " + args.get_message());
  } 

})(jQuery);

The idea is while rendering body in CSR save user id's an array and on footer render call use this Array to create targetusers payload then make this call for all users at once. Code is taken from here slightly modified as per the above requirement. check it as needed.

Note: This applies to both SPO and on-premises.

2

You could get user profile by REST API also.

For Office 365/SharePoint Online:

http://siteurl/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='LastName')?@v='i:0%23.f|membership|[email protected]'

For SharePoint 2013 On-Premise:

http://siteurl/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='LastName')?@v='domain\username'

More details here.

Your Answer

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

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