we are using Client peoplepicker in our SP custom form. Below is the code for that.

    function getUserInfo(peoplePickerElementId) {
        var toSpanKey = peoplePickerElementId+ "_TopSpan";
        var peoplePicker = null;
        var ClientPickerDict = this.SPClientPeoplePicker.SPClientPeoplePickerDict;
        for (var propertyName in ClientPickerDict) {
             if (propertyName == toSpanKey) {
                  peoplePicker = ClientPickerDict[propertyName];
        if (peoplePicker != null) {  
            // 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>';
            // Get user keys.
            var keys = peoplePicker.GetAllUserKeys();
            if(users[0].Key != null && users[0].Key != '') {
               // $('#userKeys').html(keys);
               var u=users[0].Key;
               // Get the first user's ID by using the login name.

                var userId = getUserId(users[0].Key);
function getUserId(loginName){
    var userId=-1;
    /// get the site url
    var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
    var payload = { 'logonName': loginName}; 
    /// make an ajax call to get the site user
        url: siteUrl + "/_api/web/ensureuser",
        method: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(payload),
        headers: {
             "X-RequestDigest": $("#__REQUESTDIGEST").val(),
             "accept": "application/json;odata=verbose"
        success: function (data) {
        error: function (data) {            
    return userId;

code was working fine when we were using var siteUrl = _spPageContextInfo.siteAbsoluteUrl;

in our code. But now we have changed it to var siteUrl = _spPageContextInfo.webAbsoluteUrl; as users dont have acccess to root site. so now userId=data.d.Id; no user ID is returning here. Please asssit.

  • Off topic suggestion - Do not use async: false. It is a bad practice. Try to use js promises instead. – Raf Mar 6 at 4:24
  • I tried your code on SP 2013 - It works with _spPageContextInfo.siteAbsoluteUrl and _spPageContextInfo.webAbsoluteUrl. No issues. I suggest to rework your code by this way(see answer) - stackoverflow.com/questions/33259576/… – Raf Mar 6 at 5:45
  • @Raf it started working with async:false, somehow. i dont have in depth knowledge of javascript, any clue why it was not working without that and why we should not use it in our code? – Adarsh Awasthi Mar 6 at 6:03
  • @adarsg-awasthi because this option is deprecated. It is recommend to rewrite your ajax call. stackoverflow.com/questions/11448011/… – Raf Mar 6 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.