Monday, October 29, 2012

Get and Set values in people picker control sharepoint



Sometimes we need to get all the people or groups information which is entered in People picker control of a sharepoint programatically.

For this we need to take the SPFIeldUserValue object to read that information.

Here is the sample code to do the same programatically.


private SPFieldUserValueCollection GetPeopleFromPickerControl(PeopleEditor people, SPWeb web)
{
  SPFieldUserValueCollection values = new SPFieldUserValueCollection();
  if (people.ResolvedEntities.Count > 0)
  {
    for (int i= 0; i< people.ResolvedEntities.Count; i++)
    {
        PickerEntity user = (PickerEntity)people.ResolvedEntities[i];
        switch ((string)user.EntityData["PrincipalType"])
        {
          case "User":
            SPUser webUser = web.EnsureUser(user.Key);
            SPFieldUserValue userValue = new SPFieldUserValue(web, webUser.ID, webUser.Name);
            values.Add(userValue);
          break;
 
          case "SharePointGroup":
            SPGroup siteGroup = web.SiteGroups[user.EntityData["AccountName"].ToString()];
            SPFieldUserValue groupValue = new SPFieldUserValue(web, siteGroup.ID, siteGroup.Name);
            values.Add(groupValue);                       
          break;
        }
      }
    }
    return values;
  }

Happy coding !!!




2 comments:

  1. This has saved my life this is awsome :-).. what if we have an Active directory group??.. can you please throw some light on the same

    Thanks,
    Sandy

    ReplyDelete
  2. Hello. And i don't have "PrincipalType" attribute in EntityData...

    ReplyDelete