How to Determine Entity Type in PeopleEditor Control

Because I don’t write nearly enough on this topic, I figured why not add another post about the splendid PeopleEditor control for SharePoint. Actually, these posts happen to be among my most popular. So popular in fact, that they are constantly being stolen and posted on other blogs without so much as a courtesy ping back! I won’t point any of those users out as they don’t deserve any precious link love. However, if you come across a PeopleEditor post that looks suspiciously like mine, I’m the original author!

Ok, so now that my rant is over, let’s go over a new trick I recently discovered when working with the PeopleEditor. Whenever I’ve worked with this control in the past, I’ve usually only dealt with SelectionSets that are limited to Users. So there has never been a need to handle entries where groups are specified. In case you are not familiar, here is a listing of the possible options for the SelectionSet property, posted by Alex and Ozaki on the MSDN PeopleEditor.SelectionSet Page – Community Section:

  • User = single user
  • DL = AD distribution list
  • SecGroup = AD security group
  • SPGroup = SharePoint group

I came across this requirement recently. More specifically, I needed to be able to save both users and groups, but obviously the logic for saving those is a little different. So we need a mechanism that helps us determine what type of entity a user has provided and based on that, process the correct save method.

So, let’s say we have a PeopleEditor defined as follows:

<SharePoint:PeopleEditor ID="peManager" runat="server" MultiSelect="false" Width="200px" AllowEmpty="true" SelectionSet="User,SPGroup" />

Now we want to save whatever the user enters into a column of type Person or Group that can accept both users and groups.

foreach (PickerEntity entity in peManager.ResolvedEntities)
{
    switch ((string)entity.EntityData["PrincipalType"])
    {
        case "User":
            item["Assigned"] = SavePeopleEditorControl(peManager.CommaSeparatedAccounts);
            break;
        case "SharePointGroup":
            SPGroup group = web.Groups[entity.EntityData["AccountName"].ToString()];
            item["Assigned"] = group;
            break;
        // add additional case statements for the other SelectionSets
    }
}

There you have it. Enjoy!

, ,

3 Responses to “How to Determine Entity Type in PeopleEditor Control”

  1. wizofaus February 17, 2013 at 3:38 pm #

    Doesn’t work under 2013 though, you can use the EntityType field instead.

  2. shereen February 18, 2013 at 3:30 pm #

    In 2013, we’ve been using this: http://msdn.microsoft.com/en-us/library/jj713593.aspx

  3. geek September 5, 2013 at 12:28 pm #

    wizofaus you are wrong :(

    this works in sp2013 but only if you don’t have ad connection…

    adding this will help

    if (user.ProviderName.Equals(“AD”)) {
    switch ((string)user.EntityType)
    {
    case “User”:
    SPUser webUser = web.EnsureUser(user.Key);
    SPFieldUserValue userValue = new SPFieldUserValue(web, webUser.ID, webUser.Name);
    usercollection.Add(userValue);
    break;

    case “SecGroup”:
    SPGroup siteGroup = web.SiteGroups[user.EntityData[“AccountName”].ToString()];
    SPFieldUserValue groupValue = new SPFieldUserValue(web, siteGroup.ID, siteGroup.Name);
    usercollection.Add(groupValue);
    break;
    }
    }

Leave a Reply