1

I'm using the SP PeoplePicker posted in this Microsoft article: https://msdn.microsoft.com/en-gb/library/office/jj713593.aspx

But how can I load the user ID's into a promise? I'm not familiar with the jquery promise method and the explanations found are quite hard to understand.

P.S.: I need the ID's to get passed to CDR Rest Call and posted into a user field list

Code so far:

function getUserId(loginName) {

var deferred = $.Deferred();

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

return deferred.promise();};

EDIT: So this is my PeoplePicker with the promise. I'm trying to get multiple ID's at once, the standard PplPicker only gives out the first ID

    // 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.
    $.when(getUserId(user.key))
    .then(function (result) {
        $('#userId').html(result.get_id());
    });
}

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

    var deferred = $.Deferred();

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

    return deferred.promise();
};
0

Call the getUserId function as shown below:

$.each(users, function (key, user) {
    $.when(getUserId(user.key))
    .then(function(result){
        console.log(result.get_id());
    });
});

Basically, when the promise is returned, use get_id() method to get the user id. You can pass this ID to the REST call to store it in a People or Group column.

  • the code doesn't work, I neither get an error message, nor is the Id posted in console log. I'm really desperated everything I tried so far doesn't work :( – Jay T. Aug 29 '16 at 12:28
  • Did you put a breakpoint and check what is the value returned to "result" parameter? Also did you check the value of user.key? – Suresh C Aug 30 '16 at 14:57
  • when calling the getUserId function it's giving me the "user is not defined" error – Jay T. Aug 31 '16 at 5:49
  • @Jay, can you share the complete code? – Suresh C Aug 31 '16 at 7:27
  • I edited the code above – Jay T. Aug 31 '16 at 8:08

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.