One of the sorely missing pieces in the Silverlight toolbox for SharePoint developers is a people picker control. There is a control that SharePoint provides, which you can use in a web part or custom layouts page (the PeopleEditor class). Silverlight code though all executes on the client side, so there isn’t an out of the box solution for this. I really needed this functionality on a recent project and ended up finding a fairly simple way to build just such a thing. Here’s a screenshot of my admittedly No-UI-Talent picker control, implemented as a popup in Silverlight:
The key to making this thing work is a nice little web service that SharePoint ships out of the box called the People web service. In this case I used method called SearchPrincipals to take the text that was typed in the Search box and find a list of possible matches. In my simple case I only needed to find one and one only user. However I could have also asked for groups, distribution lists, or SharePoint security groups. You could also implement your own version of the type-in control by letting someone type in a name and then calling the ResolvePrincipals method on the web service. Here are a few of the implementation details that you might find interesting:
- The method returns an ObservableCollection of PrincipalInfo objects. To make it more user-friendly I created a custom class called PickerEntry that contained a DisplayName and AccountName. The PrincipalInfo class also includes properties for Department and Email, so that may also be useful to you. In this particular case, I overrode the ToString() method in my class and had it return the DisplayName property, which is how I got the names to show up as desired in the list box. That’s also how I determined what the selected user’s account name is when selected.
- I wanted to be able to use the web service entry point from the current SharePoint site I was in. That was a little more challenging, or at least not obvious, to do from Silverlight. Fortunately I was able to tackle that with this code:
//get info on the current host
string curUrl = HtmlPage.Document.DocumentUri.AbsoluteUri.ToString();
//get the host name; note that this assumes the user has rights to the root site
//site collection; that may not be true in your scenario
Uri curUri = new Uri(curUrl);
HostName = curUri.Scheme + "://" + curUri.Host + ":" + curUri.Port.ToString();
//use the host name property to configure the request against the site in
//which the control is hosted
PeopleWS.PeopleSoapClient ps = new PeopleWS.PeopleSoapClient();
new System.ServiceModel.EndpointAddress(HostName + "/_vti_bin/People.asmx");
Actually that’s probably the only particularly interesting part. I’m attaching the code for the user control to this posting. Unfortunately for a variety of reasons I can’t include the entire project, but hopefully this post and the control code itself will be enough to get you pretty far down the road should you need something like this.