0

So with the help of Suresh C I tried to get multiple Id's at once from the PeoplePicker SP app. Standard code only gives out the Id of the first user. I'm going thorugh my code over and over again, but I still can't find any error or mistake.

 // Run your custom code when the DOM is ready.
$(document).ready(function () {

    // Specify the unique ID of the DOM element where the
    // picker will render.
    initializePeoplePicker('peoplePickerDiv');
});

// Render and initialize the client-side People Picker.
function initializePeoplePicker(peoplePickerElementId) {

    // Create a schema to store picker properties, and set the properties.
    var schema = {};
    schema['PrincipalAccountType'] = 'User,DL,SecGroup,SPGroup';
    schema['SearchPrincipalSource'] = 15;
    schema['ResolvePrincipalSource'] = 15;
    schema['AllowMultipleValues'] = true;
    schema['MaximumEntitySuggestions'] = 50;
    schema['Width'] = '280px';

    // Render and initialize the picker. 
    // Pass the ID of the DOM element that contains the picker, an array of initial
    // PickerEntity objects to set the picker value, and a schema that defines
    // picker properties.
    this.SPClientPeoplePicker_InitStandaloneControlWrapper(peoplePickerElementId, null, schema);
}

// Query the picker for user information.
function getUserInfo() {

    // Get the people picker object from the page.
    var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;

    // Get information about all users.
    var users = peoplePicker.GetAllUserInfo();
    var userInfo = '';
    for (var i = 0; i < users.length; i++) {
        var user = users[i];
        for (var userProperty in user) {
            userInfo += userProperty + ':  ' + user[userProperty] + '<br>';
        }
    }
    $('#resolvedUsers').html(userInfo);

    // Get user keys.
    var keys = peoplePicker.GetAllUserKeys();
    $('#userKeys').html(keys);

    // Get the user's ID by using the login name.
    $.each(users, function (key, user) {
        $.when(getUserId(user.key)).then(function (result) {
            $('#userId').html(result.get_id());
        });
    });
}

// Get the user ID.
function getUserId(loginName) {

    var deferred = $.Deferred();

    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {

        var context = new SP.ClientContext.get_current();
        this.user = context.get_web().ensureUser(loginName);
        context.load(this.user);
        context.executeQueryAsync(

             Function.createDelegate(this,
                function () { deferred.resolve(this.user); }),
            Function.createDelegate(this,
                function (sender, args) { deferred.reject(args.get_message()); })

             );

    });
    return deferred.promise();
};

Executing this just gives out nothing for userId, also I don't get any error when I'm debugging it. Any advice or help how to get this to work?

2
// Run your custom code when the DOM is ready.
$(document).ready(function () {    
initializePeoplePicker('peoplePickerDiv');
});

// Render and initialize the client-side People Picker.
function initializePeoplePicker(peoplePickerElementId) {

// Create a schema to store picker properties, and set the properties.
var schema = {};
schema['PrincipalAccountType'] = 'User,DL,SecGroup,SPGroup';
schema['SearchPrincipalSource'] = 15;
schema['ResolvePrincipalSource'] = 15;
schema['AllowMultipleValues'] = true;
schema['MaximumEntitySuggestions'] = 50;
schema['Width'] = '280px';  
this.SPClientPeoplePicker_InitStandaloneControlWrapper(peoplePickerElementId, null, schema);
}

// Query the picker for user information.
function getUserInfo() {

// Get the people picker object from the page.
var peoplePicker =     this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;

// Get information about all users.
var users = peoplePicker.GetAllUserInfo();
var userInfo = '';
for (var i = 0; i < users.length; i++) {
// Get the user's ID by using the login name.
$.when(getUserId(users[i].Key))
.then(function (result) {
    $('#userId').html(result.get_id());
});
    var user = users[i];
    for (var userProperty in user) {
        userInfo += userProperty + ':  ' + user[userProperty] + '<br>';
    }
}
$('#resolvedUsers').html(userInfo);

// Get user keys.
var keys = peoplePicker.GetAllUserKeys();
$('#userKeys').html(keys);    
}

// Get the user ID.
function getUserId(loginName) {

var deferred = $.Deferred();

var context = new SP.ClientContext.get_current();
var getUserId = context.get_web().ensureUser(loginName);
context.load(getUserId );
context.executeQueryAsync(
     Function.createDelegate(this,
        function () { deferred.resolve(getUserId ); }),
    Function.createDelegate(this,
        function (sender, args) { deferred.reject(sender, args); }));

return deferred.promise();
};

Execute the code will work work for you.

  • Thank you, but when I change the code giving me out the results in console log, I can see that I only get the id of the last typed-in user twice. – Jay T. Sep 2 '16 at 8:51
  • Debugging reports the following error: "0x800a139e - The property or field 'Id' (Points to "console.log(result.get_id());") has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested." – Jay T. Sep 2 '16 at 8:56
  • Ok, dumb mistake: in "getUserId" function, I had to remove "this." from user variable, now it is showing me the correct Id's in console.log – Jay T. Sep 2 '16 at 9:18
0

So here's the full working code:

// Run your custom code when the DOM is ready.
$(document).ready(function () {
    initializePeoplePicker('peoplePickerDiv');
});

// Render and initialize the client-side People Picker.
function initializePeoplePicker(peoplePickerElementId) {

    // Create a schema to store picker properties, and set the properties.
    var schema = {};
    schema['PrincipalAccountType'] = 'User,DL,SecGroup,SPGroup';
    schema['SearchPrincipalSource'] = 15;
    schema['ResolvePrincipalSource'] = 15;
    schema['AllowMultipleValues'] = true;
    schema['MaximumEntitySuggestions'] = 50;
    schema['Width'] = '280px';
    this.SPClientPeoplePicker_InitStandaloneControlWrapper(peoplePickerElementId, null, schema);
}

// Query the picker for user information.
function getUserInfo() {

    // Get the people picker object from the page.
    var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;

    // Get information about all users.
    var users = peoplePicker.GetAllUserInfo();
    var userInfo = '';
    for (var i = 0; i < users.length; i++) {
        // Get the user's ID by using the login name.
        $.when(getUserId(users[i].Key))
        .then(function (result) {
                console.log(result.get_id());
        });
        var user = users[i];
        for (var userProperty in user) {
            userInfo += userProperty + ':  ' + user[userProperty] + '<br>';
        }
    }
    $('#resolvedUsers').html(userInfo);

    // Get user keys.
    var keys = peoplePicker.GetAllUserKeys();
    $('#userKeys').html(keys);
}

// Get the user ID.
function getUserId(loginName) {

    var deferred = $.Deferred();

    var context = new SP.ClientContext.get_current();
    var user = context.get_web().ensureUser(loginName);
    context.load(user);
    context.executeQueryAsync(
         Function.createDelegate(null,
            function () { deferred.resolve(user); }),
        Function.createDelegate(null,
            function (sender, args) { deferred.reject(sender, args); }));

    return deferred.promise();
};

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.