0

How do I get value from ClientPeoplePicker using JQuery. I have tried below code, but not getting the results.

function getPickerInputElement(identifier) {
    var tags = document.getElementsByTagName(‘DIV’);
    for (var i=0; i < tags.length; i++) {
        var tempString = tags[i].id;
        if ((tempString.indexOf(identifier) > 0) && (tempString.indexOf(‘UserField_upLevelDiv’) > 0)){
            var innerSpans = tags[i].getElementsByTagName(“SPAN”);
            for(var j=0; j < innerSpans.length; j++) {
                if(innerSpans[j].id == ‘content’) {
                    return innerSpans[j].innerHTML;
                }
            }
        }
    }
    return null;
}
3
  • Can you please add picker html code May 29, 2015 at 12:31
  • 1
    Is this code copied from your code or from the site? If it's is your literal code, try changing the double quotes around “SPAN” to "SPAN" (goes for all double quotes and also the single quotes like around ‘DIV’ to 'DIV' (also change everywhere).
    – Quatic
    May 29, 2015 at 12:51
  • @wjervis there was a link to the code sample which is now posted in the quetion.
    – Quatic
    May 29, 2015 at 12:54

3 Answers 3

5

Try

var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
var users = peoplePicker.GetAllUserInfo();
for (var i = 0; i < users.length; i++) {
    alert(user.DisplayText);        
}
1

With this code you can get every picker's value:

var ClientPickerDict = SPClientPeoplePicker.SPClientPeoplePickerDict;
for (var propertyName in ClientPickerDict) {
    console.log(ClientPickerDict[propertyName].GetAllUserInfo());
}

Here is an utility function i use - but you need to know the id.

GetPeoplePickerValue: function (elementID) {
    /// <summary>
    ///  //returns a object with all users and their informations
    /// </summary>
    /// <param name="elementID">peoplepicker ID</param>
    var toSpanKey = elementID + "_TopSpan";
    var peoplePicker = null;

    // Get the people picker object from the page.
    var ClientPickerDict = SPClientPeoplePicker.SPClientPeoplePickerDict;
    for (var propertyName in ClientPickerDict) {
        if (propertyName == toSpanKey) {
            peoplePicker = ClientPickerDict[propertyName];
            break;
        }
    }
    if (peoplePicker != null) {
        // return information about all users.
        return peoplePicker.GetAllUserInfo();
    }
    else
        return '';
}'
0

Here are two methods:

This one uses spservices. spservices looks at the peoplepicker object and fetches back a little chart full of all the user data; you then use a little bit of DOM navigation on that chart to fetch the info you want:

var pp = $().SPServices.SPFindPeoplePicker({  peoplePickerDisplayName: whatDisplayName }).row.find("span[class='ms-entity-resolved']").attr("title");   

This one is just plain jquery (which is alot easier):

var pp = $("div[title='People Picker']").text().trim();
6
  • I was looking for other atributes , in SP 2010 But am unable to extract email, sipaddress,department etc.
    – samolpp2
    Feb 2, 2016 at 10:15
  • Have you tried spservices? It returns a javascript object and you have to search the object for the info.
    – bgmCoder
    Feb 2, 2016 at 15:00
  • ar pp2 = $().SPServices.SPFindPeoplePicker({ peoplePickerDisplayName: whatDisplayName }).row.find("div[id='divEntityData']") .attr("displaytext"); //alert( 'the pp title friom 2222 ....' + pp); //alert( 'the pp email friom 2222 ....' + pp2);
    – samolpp2
    Feb 2, 2016 at 15:10
  • i tried the above , but am not able to extract the emaiid, sipaddress,department etc. I have got that innerhtml[0] using spservices code. here is my thread link sharepoint.stackexchange.com/questions/168818/…
    – samolpp2
    Feb 2, 2016 at 15:16
  • 1
    Marc here uses a trick for extracting data: sympmarc.com/2012/04/22/…
    – bgmCoder
    Feb 2, 2016 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.