PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Custom SharePoint People Picker

Posted by PANVEGA on February 22, 2008

In this example I wanna show you how to customize the SharePoint people picker in a custom webpart with some additional controls.

SharePoint provides a control known as a “people picker” that allows users to search for and select users defined at some specified scope. This control is normally associated with Personnel field in a SharePoint List.

The control is actually called a “PeopleEditor” and is in the ‘Microsoft.SharePoint.WebControls‘ namespace. You will need to add a reference to ‘Microsoft.Sharepoint.dll’ to your project in Visual Studio, which is something you probably already have if you are writing anything that works with SharePoint.

Here’s a list of the people picker properties that can be set ()

peoplepicker-activedirectorysearchtimeout
peoplepicker-distributionlistsearchdomains
peoplepicker-nowindowsaccountsfornonwindowsauthenticationmode
peoplepicker-onlysearchwithinsitecollection
peoplepicker-searchadcustomquery
peoplepicker-searchadforests

Note:

The People Picker can only do wildcard searches for AD Windows group names. With any role provider other than the out of the box AD one the role provider lookup is specific to the entire name. For example if you have a group called “Readers” and you enter “Read” in the People Picker search dialog, it will not find your group; if you enter “Readers” it will. The role provider doesn’t provide a good way to do wildcard group search. See “Multiple Auth Providers” for more info. As well Steven Fowler in figuring out a custom membership provider has some great insights, another post People Picker and Custom Membership provider has dev insights on the GetUser() method.

The code creates three controls – a PeopleEditor (from the Microsoft.SharePoint.WebControls namespace), a button and a textbox. The button has an click event that itirates over the users picked in the PeopleEditor and writes them to the text box for instance with a stringbuilder. However you can add any control you are looking for.

First, create a custom webpart in visual studio. Read my post first develope-a-custom-webpart-in-moss.

public class Picker : System.Web.UI.WebControls.WebParts.WebPart
{

PeopleEditor pEditor;
TextBox t;
Button b;

//first initalize the controls and add them to the Controls Collection, in order to make them visible in the webpart

protected override void CreateChildControls()
{
base.CreateChildControls();
pEditor= new PeopleEditor();
this.Controls.Add(
pEditor);
b = new Button();
b.Text = “Click to see the users”;
this.Controls.Add(b);

b.Click += new EventHandler(button_Click);

t = new TextBox();
t.TextMode = TextBoxMode.MultiLine;
this.Controls.Add(t);
}

void button_Click(object sender, EventArgs e)
{
try
{
ArrayList aAccount = new ArrayList();
aAccount = pe.Accounts;

//store all selected users from the people picker in th arry peEntities
ArrayList peEntities = pEditor.Entities;

System.Text.StringBuilder builder;

for (int i = 0; i < pe.Entities.Count; i++)
{
// 2. cast object Entities in PickerEntity class
PickerEntity pickEn = (PickerEntity)peEntities[i];
builder = new System.Text.StringBuilder();

//add the user properites to the stringbuilder when the name is resolved
if (pickEn.IsResolved)
{
builder.AppendLine(pickEn.DisplayText);
builder.AppendLine(pickEn.Description);
builder.AppendLine(Convert.ToString(hstEntityData[“DisplayName”]));
//string email = Convert.ToString(hstEntityData[“Email”]);
//string loginName = pickEn.Key;
builder.AppendLine(Convert.ToString(hstEntityData[“SPUserID”]));
//string department = Convert.ToString(hstEntityData[“Department”]);
builder.AppendLine(Convert.ToString(hstEntityData[“PrincipalType”]));
//string sIPAddress = Convert.ToString(hstEntityData[“SIPAddress”]);
//string title = Convert.ToString(hstEntityData[“Title”]);

t.Text = builder.ToString();
}
}

}
catch (Exception ex)
{
// Manage error event
throw(ex);
}

}

It look like this in sharepoint:

Tip:

Debug your custom webpart by attaching the process IIS w3wp.exe in order to see the Entities and properties in the item.

Now, how can we customize this to our form’s needs? There are more options, but you will have to play around with them yourself.

  • use “.AllowEmpty” to specify if the user must fill the contorl
  • use “.AllowTypeIn” to specify if the user can type the wanted user name in the text box, or if he must use the search pop up
  • use “.MultiSelect” to specify if the user can select multiple people in the control.
  • use “.PrincipalSource” to specify where the control will look for users (if you have more than one source of users…)
  • use “.PlaceButtonsUnderEntityEditor” to specify if the “check name” and “address book” buttons will be on the same line with the text box
  • use “.SharePointGroup” to filter the people the user can select to be only people from a specific group defined in the site.

Note:

When the sharepoint is installed in environment with multiple domains , the user profile import mechanism only imports the user from the domain in which the sharepoint server is installed.

I recently came across the scenario where a client was managing multiple domains for various business units in the company. They had deployed SharePoint to domain X, and allowed users from Domain Y to authenticate and use the portal.

They noticed that when users tried to use the people picker control to select users from Domain Y, they were unable to find the users. It appears that the people picker control defaults to only searching in the domain where the farm is deployed. Adding some additional configuration via STSADM was the way to go.

Command:

stsadm -o setproperty -url http://CentralAdminURL:8888 -pn “peoplepicker-searchadforests” –pv “domain:DomainX.company.net;domain:DomainY.company.net”

References:

http://blogs.msdn.com/sharepoint/archive/2006/03/15/552331.aspx

Follow this post to come arround with this issue http://metahat.blogspot.com/2007/06/people-picker-in-moss-2007-unable-to.html

Advertisements

2 Responses to “Custom SharePoint People Picker”

  1. Is there any way in people picker to allow search only for specific users?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: