Quick Tip – Programmatically Returning all Distribution Groups in AD within a C# project…

I am working on a new application at the moment which requires for all Distribution Groups within an Exchange 2010 environment to be populated into a Drop Down list on the default ASP.NET page, and then based upon the selected Distribution group place the SMTP addresses of each member of that group into a list box.

Having a hunt all over the web to see if this has been done before (well I say “all over the web”, maybe about 0.016% of it!) – I could not find a code example which quite met my needs.

Therefore I decided to have a crack at it myself – and to my surprise came up with the following code which works! (I am not saying that it is the most efficient way of doing it – just that it works).

If you are interested in using the code sample below, you will need to start a new C# project in Visual Studio, and ensure that the you have imported the “System.DirectoryServices” reference into your project – this is accomplished by adding the reference at the top of your main code file – see below

using System.DirectoryServices;

You will also need to add the following Controls for your Form:

  • A drop down list control – called dd_Membership
  • A List control – called lb_Members

The main part of the code uses LDAP filters within the DirectorySearcher object to perform the following actions:

  • Locate all Groups in the Directory that are of the type “Distribution” – this uses the filter:
    (&(objectCategory=group)(!groupType:1.2.840.113556.1.4.803:=2147483648))
    
  • Locate all members of the selected distribution group using the following filter:
    (&(objectCategory=person)(|(objectClass=contact)(objectClass=user))(memberOf=" + dd_Membership.Text + "))
    

The code looks which populates both the Distribution list and get the membership to the list box looks like the following:

public static DirectoryEntry GetDirectoryEntry()
       {
           try
           {
               DirectoryEntry entryRoot = new DirectoryEntry("LDAP://RootDSE");
               string Domain = (string)entryRoot.Properties["defaultNamingContext"][0];

               DirectoryEntry de = new DirectoryEntry();

               de.Path = "LDAP://" + Domain;
               de.AuthenticationType = AuthenticationTypes.Secure;

               return de;
           }
           catch
           {
               return null;
           }
           
       }

       protected void Page_Load(object sender, EventArgs e)
       {

           DirectoryEntry entry = GetDirectoryEntry();
           DirectorySearcher search = new DirectorySearcher(entry);
           search.Filter = "(&(objectCategory=group)(!groupType:1.2.840.113556.1.4.803:=2147483648))";
           SearchResultCollection results;
           results = search.FindAll();

           foreach (SearchResult ent in results)
           {
               dd_Membership.Items.Add(ent.Properties["DistinguishedName"][0].ToString());
           }

           search.Filter = "(&(objectCategory=person)(|(objectClass=contact)(objectClass=user))(memberOf=" + dd_Membership.Text + "))";
           results = search.FindAll();

           lb_Members.Items.Clear();

           foreach (SearchResult ent in results)
           {

               lb_Members.Items.Add(ent.Properties["mail"][0].ToString());
           }
       }

Which produces results like the following within your application:

distListcSharpeExample

Of course there might be more efficient ways to accomplish the above (which if you have come across, please feel free to link them in the comments section) – but I thought that this might help someone out along the way.

Sharing is caring!:

3 thoughts to “Quick Tip – Programmatically Returning all Distribution Groups in AD within a C# project…”

  1. Does this handle nested groups?
    How about dynamic distribution groups?

    I recently had to write a Powershell script to dump out all members of a distribution group which contained other distribution groups, security groups and dynamic distribution groups. It wasn’t as easy as it sounds!

Leave a Reply

Your email address will not be published. Required fields are marked *