Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

PeopleEditor Control

2.81/5 (12 votes)
15 Jan 2008CPOL2 min read 3   1  
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.”

image001_small.PNG

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().

C#
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;
    }
}
  1. 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.
  2. 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).
  3. 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.
  4. To easily and quickly show data with a grid, we are going to load an ArrayList with our custom class.
  5. Finally, perform grid binding to the data source.

For further precision, see the following image of the PickerEntity object model with AD data.

image003.png

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)