Monday, January 31, 2011

SharePoint 2010 People Picker control

Introduction:
In this article we will discuss how we can use SharePoint 2010 People Picker control in a Visual Web Part.
Description:
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.Controls.Add(pplEditor);

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=12.0.0.0, 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.

2 comments:

  1. If you want to show ads about your company then let me know !!!

    ReplyDelete
  2. People Picker is a complicated control and in a way difficult too as it contains many controls on the site. In this term People Picker is a bit tricky to deal with too.

    ReplyDelete