다음을 통해 공유


Writing A SharePoint 2010 People Picker Control for Silverlight

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();

ps.Endpoint.Address =

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.

SL_PeoplePicker.zip

Comments

  • Anonymous
    January 01, 2003
    Hi Steve, I'm curious why you didn't use the SharePoint Silverlight Client Object Model instead of the service.

  • Anonymous
    January 01, 2003
    The comment has been removed

  • Anonymous
    January 01, 2003
    The comment has been removed

  • Anonymous
    August 17, 2011
    Hi Steve The attached SL_PeoplePicker.zip don't have the definition for the PeopleWS webservice definition.It would be really helpful if you caould attach the code snippet for the same. Many thanks in advance.

  • Anonymous
    March 26, 2012
    Very useful article. Thanks, Rajan

  • Anonymous
    October 23, 2013
    Hi Steve, Thanks for the post this is what i also need in my current project. I am new to silverlight could you please post the complete solution if  have or mail me Thanks in advance Ankur

  • Anonymous
    September 18, 2014
    The comment has been removed

  • Anonymous
    January 08, 2015
    m88 : http://m88en.com
    M88.com offer online sports games Asia, Sports Betting Asia, Sports Betting Sites Asia.
    m88asia : http://m88en.net
    Link to M88BET phone: m88en.com. – Register and Open Betting Account and Membership M88BET.
    m88bet : http://www.linkm88vip.com
    MANSION88 the house is one of the largest and most prestigious. Appeared quite early in the Asian market, the so-MANSION88 currently attracts more players.
    link m88 : http://m88wiki.com
    Home the M88 is the official sponsor of the football club in the Premier League
    Wish you happy with the new M88
    m88 casino online : http://m88free.com