PeopleEditor Control






2.81/5 (12 votes)
How to get data from PeopleEditor Control
Introduction
The PeopleEditor
control belongs to the SharePoint 2007 family of controls and permits WebParts to interact with ActiveDirectory through research and selection of AD objects, respecting a basic MOSS user interface. This article's scope isn’t principally to illustrate the characteristics of the control class (for that, it is enough to visit Microsoft's MSDN), but rahter the basic use in a WebPart. On top of that, it also aims to discuss the necessary code to read properties' AD objects that a user has selected, code that makes use of the PickerEntity
class.
Use Case Description
The sample will show the PeopleEditor
control, with a user that chooses from among available objects in ActiveDirectory (for instance, 2 domain users). Then we will examine the code written for a button click event to load a grid with AD objects' data.
WebPart
The WebPart by graphical view is shown in the following image. This happens after the user has selected some objects in AD and loaded data into the grid, clicking the button “Read entity.”
The Code
The code is written in a simple manner to focus attention on the use of the PeopleEditor
control. The button and the grid (GridView control) are used just to display AD objects' data and thus are only to support the sample. For this, interesting code is isolated in a separate routine named GetEntities()
, which is called by the button click event. The following shows the routine GetEntities()
.
private bool GetEntities()
{
try
{
// 1. following 2 lines load data into Entities ArrayList
ArrayList
aAccount = new ArrayList();
aAccount = _PE.Accounts;
//-------------------------------------------------------
ArrayList peEntities = _PE.Entities;
ArrayList AL = new ArrayList();
for (int i = 0; i < _PE.Entities.Count; i++)
{
// 2. cast object Entities in PickerEntity class
PickerEntity pickEn = (PickerEntity)peEntities[i];
Hashtable hstEntityData = pickEn.EntityData;
// 3. clsPickEntity is a custom class to store data
clsPickEntity cPickEnt = new clsPickEntity();
cPickEnt.DisplayName = Convert.ToString(hstEntityData["DisplayName"]);
cPickEnt.Email = Convert.ToString(hstEntityData["Email"]);
cPickEnt.LoginName = pickEn.Key;
cPickEnt.SPUserID = Convert.ToString(hstEntityData["SPUserID"]);
cPickEnt.Department = Convert.ToString(hstEntityData["Department"]);
cPickEnt.PrincipalType = Convert.ToString(hstEntityData["PrincipalType"]);
cPickEnt.SIPAddress = Convert.ToString(hstEntityData["SIPAddress"]);
cPickEnt.Title = Convert.ToString(hstEntityData["Title"]);
// 4. load custom class to custom ArrayList
AL.Add(cPickEnt);
}
// 5. fill data into GridView
_GV.AutoGenerateColumns = true;
_GV.DataSource = AL;
_GV.DataBind();
return true;
}
catch (Exception ex)
{
// Manage error event
return false;
}
}
- To start, it is necessary call the
Accounts
method of thePeopleEditor
object. This is out of our scope, but it is useful to force the loading ofArrayList
entitites which otherwise -- probably a bug of thePeopleEditor
control -- will not always be properly filled. - At this point, it is possible to cycle inside
ArrayList
entities exposed by thePeopleEditor
control and mostly set casting of an item as aPickerEntity
object (for details, see this). - For more code usability, it is convenient to assign the Hashtable
EntityData
of thePickerEntity
object to a custom class, mapping in this way the object's properties to our custom class properties. - To easily and quickly show data with a grid, we are going to load an
ArrayList
with our custom class. - Finally, perform grid binding to the data source.
For further precision, see the following image of the PickerEntity
object model with AD data.

Conclusion
The PeoplePicker
object used together with PickerEditor
is useful to allow a SharePoint user to interact with ActiveDirectory from SharePoint. It also makes an important functionality available to the developer in an easy way, by writing just a few code lines.
History
- 15 January, 2008 -- Original version posted