How to Bind Distribution List to Sharepoint Peoplepicker

I guess most of you aware of  how to use Sharepoint PeoplePicler control in your sharepoint solutions.If not here is a perfect blog http://karinebosch.wordpress.com/sharepoint-controls/peopleeditor-control/ i found while i was struggling. As it says we can bind either, User, SPGroup or SPSecurityGroup. But the real game starts when you are an amateur to Sharepoint and so badly wanted to bind a DL to Peoplepicker and use. So here is the way i came across.

Senario:

Add a DL using PeoplePicker and store it in a List. Then later get the users of the DL  (Eg: mail address etc.)

Solution:

First add the following Line to the Picker control. Note the Selection set.

<sharepoint:PeopleEditor ID="PeopleEditor2" runat="server" AllowEmpty="true" MultiSelect="true"
 SelectionSet="User,DL" CssClass="event-invite-ppl" />

Then add the following Code to determine whether it is a ‘User’ or a ‘DL’.

StringBuilder groupNames = new StringBuilder(string.Empty);
for (int i = 0; i < PeopleEditor2.Entities.Count; i++)
{
 PickerEntity pe = (PickerEntity)PeopleEditor2.Entities[i];
 if (pe.EntityType == "DL")
{
 groupNames.Append(pe.DisplayText);
 groupNames.Append(";");

}
 else if (pe.EntityType == "User")
  {
   SPFieldUserValue inviteetoadd = new SPFieldUserValue(oSPWeb, oSPWeb.EnsureUser(pe.Key).ID, pe.Key);
   inviteeListCollection.Add(inviteetoadd);
  }
}
newParticipantItem["InvitedGroupNames"] = groupNames.ToString();

Now have the Display Name of the DL. So we need to get the DL related to it and bind it to the PeoplePicker control.

if (participantListItem["InvitedGroupNames"] != null)
{
 string[] groupNames = participantListItem["InvitedGroupNames"].ToString().Split(';');
 groupNames = groupNames.Where(x => !string.IsNullOrEmpty(x)).ToArray();
 
foreach (string group in groupNames)
 {
   PickerEntity groupEntity = new PickerEntity();
   groupEntity.Key = GetADGroupName(groupName);
   entityArrayList.Add(groupEntity);
 }
}
 PeopleEditor2.UpdateEntities(entityArrayList);
 
---------------------------------------------------------------


string GetADGroupLoginName(string groupName)
 {
 string userNames = string.Empty;
 System.DirectoryServices.SearchResult result;
 DirectorySearcher search = new DirectorySearcher();
 search.Filter = String.Format("(cn={0})", groupName);
 result = search.FindOne();

 if (result != null)
 {
 userNames = result.Properties["samaccountname"][0].ToString();
 userNames = string.Format(@"<>\{0}", userNames);
 }
 return userNames;
 }

Using System.DirectoryService; //You will need to namespace 

If you need to get the names (Eg: Dayan Sameera) of the given DL users. Use the following method.

ArrayList GetADGroupUsers(string groupName)
 {
 System.DirectoryServices.SearchResult result;
 DirectorySearcher search = new DirectorySearcher();
 search.Filter = String.Format("(cn={0})", groupName);
 search.PropertiesToLoad.Add("member");
 result = search.FindOne();
 ArrayList userNames = new ArrayList();
 if (result != null)
 {
 for (int counter = 0; counter < result.Properties["member"].Count; counter++)
 {
 string user = (string)result.Properties["member"][counter]; 
 // here i needed to get only the name of the user,i used user.Substring();
 user = user.Substring(user.IndexOf("=") + 1, user.IndexOf(',') - 3);
 userNames.Add(user);
 }
 }
 return userNames;
 }

Note: Why i used the ‘Dispayname’ of the DL  ? Can we add the DL directly as a SPGroup and store ?  Please read this  and this.

Thanks.

Good luck

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s