Get multiple users from PeopleEditor and update column of type “People and Group” in SharePoint List

Get multiple users from PeopleEditor and update column of type “People and Group”.

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

<wssawc:peopleeditor id="UserPickerRequestedFor" runat="server" allowempty="False"
height="20px" multiselect="true" selectionset="User" showcreatebuttoninactivedirectoryaccountcreationmode="True"
validatorenabled="True" width="300px"></wssawc:peopleeditor>

Aspx.Cs Page:

ArrayList arrUsers = new ArrayList();
PickerEntity UserEntity = new PickerEntity();
SPFieldUserValueCollection fieldUsrValues = new SPFieldUserValueCollection();                  
int UsrCount = UserPicker.Accounts.Count;
for (int j = 0; j < UsrCount; j++)
{
    UserEntity = (PickerEntity) UserPicker.Entities[j];
    string  UserAcctName = UserEntity.Description;
    Hashtable PeopleEditorData = UserEntity.EntityData;
    string strUserLogin = UserEntity.Key;
    arrUsers.Add(strUserLogin);
}
foreach (object obj in arrUsers)
{
   string login = obj.ToString();
   SPSecurity.RunWithElevatedPrivileges(delegate()
   {
    using (SPSite elevatedSite = new SPSite(web.Site.ID))
    {
      elevatedSite.AllowUnsafeUpdates = true;
      SPWeb elevatedWeb = elevatedSite.OpenWeb(web.ID);
      {
        elevatedWeb.AllowUnsafeUpdates = true;
        SPUser User = elevatedWeb.EnsureUser(login);
        SPFieldUserValue val = new SPFieldUserValue(web,User.ID, User.Name);
        fieldUserValues.Add(val);
       }
      }
     });
   }
reqItem["UserNames"] = fieldUserValues;

Get multiple users from People Editor and update column of type “People and Group” – Method2

if (UserPickerOwners.Accounts.Count > 0)
{ reqItem["SiteOwner"] = GetUsersFromPeoplePicker(); }


protected SPFieldUserValueCollection GetUsersFomPeoplePicker(string usrType)
{
  ArrayList arrUsersMain = new ArrayList();
  ArrayList arrUsersSec = new ArrayList();
  PickerEntity UserEntity = new PickerEntity();
  SPFieldUserValueCollection fieldUsrValues = new SPFieldUserValueCollection();
  int UsrCount = 0;

  arrUsersMain = UserPickerOwners.Entities;
  UsrCount = UserPickerOwners.Accounts.Count;

  for (int j = 0; j < UsrCount; j++)
  {
    UserEntity = (PickerEntity)arrUsersMain[j];         
    string strUserLogin = UserEntity.Key;
    arrUsersSec.Add(strUserLogin);
  }      

   foreach (object obj in arrUsersSec)
   {
    string login = obj.ToString();
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
      using (SPSite elevatedSite = new SPSite(web.Site.ID))
      {
          elevatedSite.AllowUnsafeUpdates = true;
          SPWeb elevatedWeb = elevatedSite.OpenWeb(web.ID);
          {
               elevatedWeb.AllowUnsafeUpdates = true;
               SPUser User = elevatedWeb.EnsureUser(login);
               SPFieldUserValue val = new SPFieldUserValue(web, User.ID, User.Name);
               fieldUsrValues.Add(val);
           }
         }
      });
    }
   return (fieldUsrValues); 

}


No comments: