Back in the SOM (Server Object Model) days it was pretty straight forward and well documented how to gather a user’s information from within a SharePoint form, or even go out and get it from SharePoint itself having just a single piece of information like email, login name or even just the user’s first and last name. While there are techniques to doing this from the client side using CSOM (Client Side Object Model) or REST APIs what if you didn’t need ALL the user’s information and just needed a few important parts of it. If the form you are working in has a people picker then you are all set. Today I am going to show you all about getting user data from People Picker with JavaScript.
Data Included in User Object
The data you will be getting back from this call isn’t a full SPUser object or anything that detailed. You will however be able to gather a great deal of the user’s information including:
- Department
- MobilePhone
- Title
- Display Name
- Login ID
- Principal Type
- Site Collection User ID
This information is contained within the SPClientPeoplePickerEntity object that is returned in the process. Many fields are reused in the object. For example, the login ID is stored in the Description field, but it is also in the Key field. My suggestion is use the EntityData property as it contains all the fields above within it. Following is how the data is mapped:
EntityData Property | User Data |
---|---|
AccountName | Login ID in SAML format (ie. "i:0#.w|drevdev\\ddrever" |
Department | Department |
User Email | |
PrincipalType | Type of user object |
SIPAddress | User Email |
SPUserID | User's ID within the SharePoint Site Collection |
Title | Title of User (Position) |
Getting User Data from People Picker with JavaScript
In order to access the data you perform the following:
- First need to access the div the people picker is contained within.
- Cast the element as a People Picker entity using a SharePoint provided method
- Get all users listed within the People Picker
- Access an individual user’s object
- Grab data from the EntityData property within.
In the following example, I have written a small function that accesses the div based on the title of the field and returns the email address. The function is very basic and doesn’t provide any error handling, it’s just showing you the basics of how to access the data.
function getEmailFromPeoplePicker(title) { //Get the people picker field var ppDiv = $("div[title='" + title + "']")[0]; //cast the object as type PeoplePicker var peoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[ppDiv.id]; //Get list of users from field (assuming 1 in this case) var userList = peoplePicker.GetAllUserInfo(); var userInfo = userList[0]; var userEmail; //The description field contains the login info without the lookup extras. In new forms this //field can be undefined depending on the field being checked. Only check if userInfo is //initialized. if(userInfo != null) { userEmail = userInfo.EntityData.Email; } return userEmail; }
And that’s it. If you need different fields, refer back to the table above the code to get the corresponding information you need. Let me know if you have any questions around this process in the comments below.
Thanks for reading!
Comments
good post. I have a question How can I get user name of my people picker named “General”? I have more people pickers before to my people picker named General.
Hi Lauren, sorry for the late response. I never noticed the comment update on my site. Not sure why you are having trouble accessing the People Picker. If your people picker is named General then the title of the div should be General and you should be able to access it directly as my code shows. However, if you are getting multiple pickers then I am not sure if you able to dynamically grab the one you need. You may simply have to determine the one you are looking for (the order) and grab that element in the array that is returned. For example if the picker you are looking for is the 3rd item then you would write your code something like this to get the correct element: var ppDiv = $(“div[title='” + title + “‘]”)[2];
Hi,
I tried returning SPUserId but it is returning undefined. Please suggest.
You’re not giving me anywhere close to enough information to assist. I would need to know at the least a code snippet of what you are doing in order to help you out. Sorry.
Hi David, I have the same problem as Arnav. When I run my script on a previously saved item then it gives me all the properties for the User Info (Email, SPUserID, AccountName, etc.) But if I edit a form and change the user in the field (without saving it), then only the Email property is available. All the rest are undefined. (Once I save/re-enter then all the properties are available for the new name.)
I thought maybe I had to resolve the new name but it’s already resolved.
Any ideas? Thanks!
Hi Eric,
I haven’t come across this, but I’ll see if I can recreate based on the information you have provided. I’ll try to do that in the near future here.
GOOD WORK!
Thank you for this helpful post!