0

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

  • You can follow the first approach in the article you mentioned. – Amit Feb 2 '17 at 9:54
  • 1
    You are not loading the props before execute? clientContext.load(userProfileProperties[i]) – Anders Aune Feb 2 '17 at 11:08
  • @AndersAune still it's not working.. – Akshay Dattatray Nangare Feb 2 '17 at 12:00
  • @Amit , I will check that – Akshay Dattatray Nangare Feb 2 '17 at 12:01
2

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)
1

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

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.