How to use the PeopleEditor Control (People Picker)
// February 17th, 2009 // Sharepoint
After trawling many blogs i thought i might aggregate the posts ive found to create an all-in-one post on how to create, retrieve values from and update a PeopleEditor Control programmatically. Thanks to the following two blog posts with the ideas and some code in creating my post:
1. How to use the PeoplePicker in Sharepoint
2. Using SharePoint controls in your own forms – PeopleEditor control
1. Creating
To Create the control in your ascx file, first add the Register tag:
@ Register TagPrefix="SharePointSD" Namespace="Microsoft.SharePoint.WebControls"<br />
Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral,<br />
PublicKeyToken=71e9bce111e9429c"
And the control itself:
sharepointsd:peopleeditor runat="server" id="txtManager" xmlns:sharepoint="#unknown"<br />
autopostback="true" allowempty="false" selectionset="User,SecGroup,SPGroup" borderwidth="1"<br />
width="200px" placebuttonsunderentityeditor="false" rows="1" /
2. Retrieving Value
<br />
private string GetPeoplePickerValue()<br />
{<br />
string strAccountName = String.Empty;<br />
for (int i = 0; i < txtPerson.ResolvedEntities.Count; i++)<br />
{<br />
PickerEntity objEntity = (PickerEntity)txtPerson.ResolvedEntities[i];<br />
SPUserInfo objInfo = new SPUserInfo();<br />
objInfo.LoginName = objEntity.Key;<br />
//strAccountName = objInfo.LoginName;<br />
// to return a sharepoint people group formatted string use...<br />
strAccountName = objEntity.EntityData["SPUserID"].ToString() + ";#" + objEntity.DisplayText.ToString();<br />
}<br />
return strAccountName;<br />
}
2. Populating Values
<br />
public void FillPeopleEditorControl(string strUserId, PeopleEditor peopleEditorControl)<br />
{<br />
try<br />
{<br />
if (!String.IsNullOrEmpty(strUserId))<br />
{<br />
strUserId = strUserId.Substring(0, strUserId.IndexOf(";"));<br />
SPUser SelectedUser = SPContext.Current.Web.AllUsers.GetByID(Convert.ToInt16(strUserId));</p>
<p> PickerEntity pe = new PickerEntity();<br />
pe.Key = SelectedUser.LoginName;<br />
pe = peopleEditorControl.ValidateEntity(pe);</p>
<p> peopleEditorControl.Entities.Add(pe);<br />
}<br />
}<br />
catch (Exception ex)<br />
{<br />
lblError.Text = ex.Message;<br />
}<br />
}








