Question

I'm working on SharePoint app, where I can get multiple users form user profile.

using below code I'm able to get property FirstName of multiple user.

'use strict';
var clientContext =  SP.ClientContext.get_current();
var userProfileProperties = [];
var targerUsers = ["i:0#.f|membership|A@Company.onmicrosoft.com","i:0#.f|membership|B@Company.onmicrosoft.com", "i:0#.f|membership|C@Company.onmicrosoft.com", "i:0#.f|membership|D@Company.onmicrosoft.com"];  

(function () {

$(document).ready(function () {
  loadUserData();    

});

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 = ["FirstName"];       
console.log(targerUsers.length);
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() {
   debugger
var messageText = "";

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

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

})();

But I'm trying to get multiple properties for multiple users in above code.

How Can I do this. For reference I used this Article

Was it helpful?

Solution

This is working, you need to load the UserProfilePropertiesForUser:

  var loadData = function() {
        var clientContext =  SP.ClientContext.get_current();
        var userProfileProperties = [];
        var targetUsers = ["i:0#.f|membership|A@Company.onmicrosoft.com","i:0#.f|membership|B@Company.onmicrosoft.com", "i:0#.f|membership|C@Company.onmicrosoft.com", "i:0#.f|membership|D@Company.onmicrosoft.com"];   

        var profilePropertyNames = ["PreferredName", "PictureURL"];

        targetUsers.forEach(function(userName, i) {

            var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, userName, profilePropertyNames);
            userProfileProperties[i] = new SP.UserProfiles.PeopleManager(clientContext).getUserProfilePropertiesFor(userProfilePropertiesForUser);

            clientContext.load(userProfilePropertiesForUser); 
            clientContext.executeQueryAsync(function() {
                userProfileProperties.forEach(function(profile) {
                    console.log(profile)
                })
            }, function(s,args) {
                console && console.log(args.get_message());
            })
        });
    }


    SP.SOD.loadMultiple(["sp.js", "userprofile"], loadData)

OTHER TIPS

Try it as below:

function CallTest() {

var tsIds = ["i:0#.f|membership|A@Company.onmicrosoft.com","i:0#.f|membership|B@Company.onmicrosoft.com", "i:0#.f|membership|C@Company.onmicrosoft.com", "i:0#.f|membership|D@Company.onmicrosoft.com"];  

 $.each(tsIds, function (index, value) {
        getItemsSync(value).then(function(results){
                console.log(results);
            },function(err){
            console.log(err.toString());
            });       
        });   
}
function getItemsSync(userId) {    

    var deferred = $.Deferred();
    var clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

    var profilePropertyNames = ["FirstName", "Department","PictureURL"];
    var userProfilePropertiesForUser = 
        new SP.UserProfiles.UserProfilePropertiesForUser(
            clientContext,
            userId,
            profilePropertyNames);


    var userProfileProperties = peopleManager.getUserProfilePropertiesFor(
        userProfilePropertiesForUser);


    clientContext.load(userProfilePropertiesForUser);
    clientContext.executeQueryAsync(function () { 
            deferred.resolve(userProfileProperties); 
    },function (sender, args) { 
        deferred.reject(sender, args); 
    });

    return deferred.promise();
}

SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
   // Make sure PeopleManager is available 
   SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', function() {
        CallTest();
  });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top