Tuesday, 29 October 2013

get all the people or groups information from People picker

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;
  }

No comments:

Post a Comment