Wednesday, August 17, 2011

Pouplate People Picker control with users and users from group

SPWeb web = SPControl.GetContextWeb(Context);

// Select the group from which users needs to be populated , Here i have chosen AssociatedOwnerGroup

SPGroup members = web.AssociatedOwnerGroup;


foreach (SPUser user in members.Users)
  {  
               // Here i need to populate users that has email id
      if(!string.IsNullOrEmpty(user.Email))  {
              
                      PickerEntity pe = new PickerEntity();
                      pe.DisplayText = user.Name;
                      pe.Key = user.LoginName;
                     PickerEntity peValid = peUsername.ValidateEntity(pe);
                     if (peValid != null)
                     peUsername.Entities.Add(peValid);
                  }
In webpart or user control you have

<cc1:peopleeditor id="peUsername" runat="server" width="442px" cssclass="title34"multiselect="true" allowempty="False" Height="50px" />

///if you are using user control  then add this tag on top of the page

<%@ Register Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"Namespace="Microsoft.SharePoint.WebControls" TagPrefix="cc1" %>