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
{
ArrayList
aAccount = new ArrayList();
aAccount = _PE.Accounts;
ArrayList peEntities = _PE.Entities;
ArrayList AL = new ArrayList();
for (int i = 0; i < _PE.Entities.Count; i++)
{
PickerEntity pickEn = (PickerEntity)peEntities[i];
Hashtable hstEntityData = pickEn.EntityData;
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"]);
AL.Add(cPickEnt);
}
_GV.AutoGenerateColumns = true;
_GV.DataSource = AL;
_GV.DataBind();
return true;
}
catch (Exception ex)
{
return false;
}
}
- To start, it is necessary call the
Accounts
method of the PeopleEditor
object. This is out of our scope, but it is useful to force the loading of ArrayList
entitites which otherwise -- probably a bug of the PeopleEditor
control -- will not always be properly filled. - At this point, it is possible to cycle inside
ArrayList
entities exposed by the PeopleEditor
control and mostly set casting of an item as a PickerEntity
object (for details, see this). - For more code usability, it is convenient to assign the Hashtable
EntityData
of the PickerEntity
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