Infrastructure

SharePoint:PeopleEditor Basics

If you want your people editor to support all sorts of groups and users, here are the basic settings and code you need.

I use private page scoped variables as an intermediary here, but that is not required.

1) I started with a workflow initiation form using the 2010 templates.  If you don’t use that, you’ll need to add the following declaration to the top of your page:
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

2) Add your peopleeditor control to the page:
<SharePoint:PeopleEditor ID="pplApprovers" width="300px" SelectionSet="User,DL,SecGroup,SPGroup" MultiSelect="true" AllowTypeIn="true" IsValid="true" runat="server" />

3) Page scoped variables:
private ArrayList approversArray;
private SPFieldUserValueCollection approversCollection;

4) Use the following code to read the data from the SharePoint list item:
//Column named “Approvers”
approversCollection = (SPFieldUserValueCollection)item[“Approvers”];
PickerEntity entity = new PickerEntity();
approversArray = new ArrayList();
foreach (SPFieldUserValue spFieldUserValue in approversCollection)
{
   entity = new PickerEntity();
   entity.Key = spFieldUserValue.LookupValue;
   entity = pplApprovers.ValidateEntity(entity);
   approversArray.Add(entity);
}

5) Use the following code to populate your control with that information:
pplApprovers.UpdateEntities(approversArray);

6) Use the following code to read the control values:
approversArray = pplApprovers.ResolvedEntities;

7) Use the following code to save the data back to the list item:
approversCollection = new SPFieldUserValueCollection();
SPUser user;
SPGroup group;
foreach (PickerEntity entity in approversArray)
{
    if (entity.EntityData["PrincipalType"].ToString() == "SharePointGroup")
    {
        group = web.SiteGroups[entity.Key];
        approversCollection.Add(new SPFieldUserValue(web, group.ID, group.LoginName));
    }
    else
    {
        //handles SecurityGroup, Distribution List and User
        user = web.EnsureUser(entity.Key);
        approversCollection.Add(new SPFieldUserValue(web, user.ID, user.LoginName));
    }
}
item[“Approvers”] = approversCollection;

3 Comments

  1. Thomas Resing August 25, 2011
  2. Joseph August 25, 2011
  3. Shiv Narayan Patel April 21, 2016

Leave a Reply