1

I've used this code (found on this site) to access a people picker field in SP2016:

var peoplePickerID = "field title"; 
var clientPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[$('[title^="' + peoplePickerID + '"]')[0].id];

Seems to work well and I've console logged the output that looks like this image below. (Have redacted info but the basics are there.)

What I need to do is get the DisplayName from the ProcessedUserList. There may be more than one name in the field.

Any ideas how to get the DisplayName?

screen cap of console

1 Answer 1

0

You can get the display name of users from people picker field using below code:

var peoplePickerID = "TestPersonField";
var clientPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[$('[title^="' + peoplePickerID + '"]')[0].id];
var peoplePickerUsersInfo = clientPeoplePicker.GetAllUserInfo();
if (peoplePickerUsersInfo.length > 0) {
    for(var index = 0; index < peoplePickerUsersInfo.length; index++) {
        console.log(peoplePickerUsersInfo[index].DisplayText);
    }
}

Output:

enter image description here


Update from comments:

If above code is not working for you, you can use below code to extract "DisplayName" from "ProcessedUserList":

var peoplePickerID = "TestPersonField";
var clientPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[$('[title^="' + peoplePickerID + '"]')[0].id];
var processedUserList = clientPeoplePicker.ProcessedUserList;
var objectKeys = Object.keys(clientPeoplePicker.ProcessedUserList);

for(var i = 0; i < objectKeys.length; i++) {
    if(processedUserList[objectKeys[i]].hasOwnProperty("DisplayName")) {
        var displayName = processedUserList[objectKeys[i]].DisplayName;
        console.log(displayName);
    }
}
9
  • It's coming up with a length = 0. Need to look inside the ProcessedUserList to get the DisplayName. How to do that?
    – whahoo
    Jul 21, 2022 at 13:58
  • Are you getting empty array while using GetAllUserInfo()? It is working fine for me (check screenshot in my answer). Where are you adding this code, new/edit form or display form of list? Jul 21, 2022 at 14:08
  • Check updated answer for fetching "DisplayName" from "ProcessedUserList". Jul 21, 2022 at 14:27
  • Here's the code: ` function getAssignedTo() { alert("we're in the getAssignedTo function"); var peoplePickerID = "Assigned To"; var clientPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[$('[title^="' + peoplePickerID + '"]')[0].id]; console.log(clientPeoplePicker); var getList = clientPeoplePicker.GetAllUserInfo(); console.log(getList); if (getList > 0) { for(var index = 0; index < getList.length; index++){ console.log(getList[index].DisplayText); } } }`
    – whahoo
    Jul 21, 2022 at 17:31
  • It should be if (getList.length > 0). You can use my code as it is. You just need to change the peoplePickerID to "Assigned To". Try both solutions given above. Jul 21, 2022 at 17:35

Your Answer

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

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