Writing Silverlight people picker control using SharePoint 2010 Client object model
Whenever you are developing a silverlight solution on SharePoint 2010, one of the basic functionality you will require is the ability to select people from your Active Directory.
Sadly, I could not find a ready made custom silverlight control which I could utilize for my solution. I decided to write on my own. Rather than writing from scratch, I decided to use the boilerplate code provided by Steve.
In this post, I am going to deep dive how I built this control.
My solution consists of two controls, PeopleChooser
and PeoplePicker
. PeopleChooser
is a user control which is configured to either single select or multi select depending upon the
configuration. Here’s a screenshot of my people Chooser control, implemented as a user control in Silverlight.
In case of multiple results, user can right click on the textbox to display multiple results and select one of them.
The multi select configuraiton is rendered as a listbox instead of a textbox. I made a conscious design decision to do this so that user instantly comes to know that this is a multi select picker by directly looking at it.
PeoplePicker
is a ChildWindow
which is launched from the PeopleChooser
to search and enumerate people from the active directory.
Clicking on the address book button opens the people picker, where the user can select either multiple people or single people depending upon the configuration of the control.
This is the screenshot of people picker control.
Feel free to download this control or customize by downloding the source from my github repo
Anyone, who wants to reuse or customize this control further, I will explain in brief how this works.
Both PeopleChooser
and PeoplePicker
have a listbox which employ two-way data binding.
Both listbox are bound to a following data structure SelectedAccounts
which is an ObservableCollection
of type AccountList
The below XAML shows the two way databinding with this data structure
SelectedAccounts
data structure is populated using People WebService.
Depending upon if multiple selection is allowed or not, ListBox
is hidden and TextBox
is shown instead along with a resolve button.
If there are muliple results, right mouse button on TextBox
is handled and displays choices to user to enable him to select a single result.