Monday, January 31, 2011

SharePoint 2010 People Picker control

In this article we will discuss how we can use SharePoint 2010 People Picker control in a Visual Web Part.
There are two ways to add people picker control into a visual web part. Either you can programmatically add into the .cs file and finally add the control to a place holder like the below. You can also check this article on how to create a Visual Web Part in SharePoint 2010.

The below code will add a people picket control in to the page
PeopleEditor pplEditor = new PeopleEditor();
pplEditor.ValidatorEnabled = true;

pnlAddColleagues -> Panel in the UI.

In the second option:
First give a reference to Microsoft.Sharepoint.WebControls

Then register like below.
<%@ Register Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WebControls" TagPrefix="cc1" %>

Then use like:
{cc1:PeopleEditor ID="BlazerID" runat="server" MaximumEntities="1"
MultiSelect="false" AllowEmpty="false"
ErrorMessage="Please Enter a User" ValidatorEnabled="true"}

You will get lot of properties here.

To get all the user details you can follow the below way

for (int i = 0; i < pplEditor.ResolvedEntities.Count; i++)
PickerEntity picker = (PickerEntity)pplEditor.ResolvedEntities[i];
Hashtable hstEntityData = picker.EntityData;
string accountName = Convert.ToString(hstEntityData["AccountName"]);
string emailID = Convert.ToString(hstEntityData["Email"]);

This will give you the account name, email id of the corresponding user that are place in the people picker. You can also visit SharePoint 2010 Object Model.