People Picker control holds a well-known reputation in SharePoint Environment as being a reliable source of selecting SharePoint Users & Groups.
Since the current & recommended SharePoint Development strategies are pointing towards the Client Side Development Techniques, discussing Client Side counterpart of People Picker Control (Libraries) becomes much more useful.
In this demo, I will be discussing the implementation details of the Client Side People Picker Control in Production Scenarios.
I developed this POC as a tool which is an addition to my toolset and often proves helpful to deal with investigating User Properties by querying User Profile Service & and basic property set associated with this control.
To start with this demo, I have added some of the UI elements as described below.
- User Selection - This section will be having Client Side People Picker Control implementation which further allows user selection.
- User ID - This section will display User ID of the selected User, which is a crucial piece of information while working out test scenarios.
- People Picker Properties - This section will display the User Properties that can be retrieved from the People Picker Objects.
- User Profile Properties - This section will display the User Properties that can be retrieved from the User Profile Service based on the selected User Account.
In the next few screenshots, we can see the HTML laid down to implement this simple UI.
Notice the Div with ID “ppOwner”. This will be the placeholder for the People Picker Control HTML replaced later on.
Div with ID “UserID” will hold the User ID information.
Div with ID “resolvedUser” will hold the People Picker Properties.
Div with ID “userProfileProperties” will hold the User Profile Properties for the selected user.
Let’s look at the code file that is implementing the logic behind this tool as described in below steps:
Include all the JS files into your code as shown in the screenshot below.
Since these files are inter-referenced, the sequence here is critical. Make sure to include the files in the same sequence.
“initializePeoplePicker” is a generic function that defines the schema settings for the Client Side People Picker control which defines the behavior of this control.
“registerPPOOnChangeEvent” is the function used to register events for People Picker Control.
This step shows the registration of “OnControlResolvedUserChanged” Event that will be executed every time a valid user is selected using the People Picker control.
Once this event is fired, it will make a call to “Execute” Function.
Inside the “Execute” function, we have to first create an object to People Picker Control. It is important to note the convention to write the code:
“this.SPClientPeoplePicker.SPClientPeoplePickerDict.” + <Enter ID of the people picker Div Element> + “_TopSpan”;
Based on this convention, we have an object created as follows:
var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.ppOwner_TopSpan;
Once the object has been initialized, we can call “GetAllUserInfo” function that will return a dictionary of User Properties in the form of Key & Values for each user selected in the People Picket (if multi-selection is enabled).
Get each of the users from the dictionary and pass it to another function “getUserDetails” that will use this to further query User Profile Properties.
Retrieving the User ID out of Properties dictionary and showing it in the User ID section.
Call User Profile REST End Point by passing username with a domain ([domainName]\[userName]) from the Step 8.
If the REST Call is successful, pass the user properties collection to the “renderUserDetails” function to display all the properties in User Profile Properties Section.
If the REST Call is failed, we can handle the exceptions in this callback function.
“getUserId” function takes the username with domain ([domainName]\[userName]) as input parameter. This function will return a promise that we are consuming in Step 9.
“renderUserDetails” function takes user properties dictionary as input from Step 11. Loop it through each of the dictionary entry and render the data in User Profile Properties Section.
And, this is all for the code.
In order to test the implementation, let's go back to the People Picker page and select the required User you would like to look for.
On selection and if this user is valid (resolved), we will able to see the User Properties as shown below.
That is all for this demo.
Hope you will find it helpful.