How to Use the PeopleEditor Control: Loading Data

This post was really planned as a Part 2 to the first post I made about saving data from the PeopleEditor control. My aim for this entry is to tackle the reverse scenario: reading data from a field of type Person or Group and loading it into a PeopleEditor control. If you have any troubles with Part 1 or Part 2 of this series or if there’s something I’ve missed, I would love to hear about it. Leave a comment or drop me an email. Thanks!

If you’ve followed Part 1 of this series, you should already have a list named Demo created with a column of type Person or Group named Managers. If not, begin by creating a new Custom List named Demo. Within List Settings, create a column of type Person or Group and name it Managers. Set the Allow multiple selections checkbox to Yes.

Once you’ve created the list and the column, add a few new entries so we have some data to load.

Now let’s create a custom application page with a single PeopleEditor control on it. Create a new page (or download it here) called Load.aspx and store it in the layouts directory.

Since the people editor control is located within the Microsoft.SharePoint.WebControls namespace, you’ll have to register the tag prefix at the top of your page:

1
<%@ Register TagPrefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

Insert the control on the page. This is the control we’ll be loading the data into:

1
<wssawc:PeopleEditor AllowEmpty="false" Width="300px" id="Managers" runat="server" SelectionSet="User" />

The next step will require that we wire up our Page_Load event to load the necessary data from the list to the control.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
protected void Page_Load(object sender, EventArgs e)
{
 
    try
    {
 
        SPWeb web = SPContext.Current.Web;
 
        // get a handle to the list we’ll be pulling values from
        SPList list = web.Lists["Demo"];
 
        // using the querystring parameter containing the id, get the list item we’ll be dealing with
        SPListItem listItem = list.GetItemById(Convert.ToInt32(Request["id"]));
 
        // the managers column is of type Person or Group, so we can use a spfielduservaluecollection to     store the values from it
        SPFieldUserValueCollection users = (SPFieldUserValueCollection)listItem["Managers"];
 
        // our array will hold the entities we’ll use to eventually assign to the people editor control
        ArrayList entityArrayList = new ArrayList();
 
        // loop through each use in the collection, set the key, add to the array
        for (int i = 0; i < users.Count; i++)
        {
 
            PickerEntity entity = new PickerEntity();
            entity.Key = users[i].User.LoginName;
            entityArrayList.Add(entity);
 
        }
 
        Managers.UpdateEntities(entityArrayList);
 
    }
 
    catch (Exception ex)
    {
 
        Response.Write(ex.ToString());
 
    }
 
}

Now when you navigate to your page, pass the id of a valid item in the list, and the entries in the manager column should load: http://servername/_layouts/Load.aspx?id=1 (replace servername with the name of your MOSS install)

, ,

4 Responses to “How to Use the PeopleEditor Control: Loading Data”

  1. Kha December 8, 2008 at 6:10 pm #

    Hi The topic’s execellent! Currently, People Editor get data from User Directory. Can it get Data from a Contact List of SharePoint??? How it do that??? Thanks.

  2. shereen December 8, 2008 at 6:10 pm #

    @Kha: can you clarify what you mean by a contact list within sharepoint? If it’s a field you want to pull from, what type of field is it? I can try to give you an example if you give me just a little more detail. Send to shereen at qumsieh dot ca and i’ll respond pretty quickly.

  3. Rajiv Giri December 8, 2008 at 6:10 pm #

    If I use SPFieldUserValueCollection users = (SPFieldUserValueCollection)listItem[“Managers”]; in my code i get an error object reference not set to an instance …However if i print the value of list item (people picker) in string format it shows a value like 22;#Managers .. Please suggest or reply through mail if you can ….

  4. Joanne Mason October 2, 2015 at 3:46 pm #

    Just excellent.
    I was able to use this with the SharePoint 2013 clientPeoplePicker.

Leave a Reply