C# Get Exchange Distribution and Dynamic Distribution Lists and their members

Hello,

Today i will be sharing how to get Exchange Distribution and Dynamic distribution list programatically using C# and Active Directory.

below is the function which enables you todo so but before that we need to create a common class for both distribution list because they have something in common which is users who has email address. normal distribution list has members while the dynamic distribution list members is being auto generated based on LDAP Query.

first the common Class

/// <summary>
/// This enum will hold the type of a Distrinution List 
/// DL for Normal Distribution List 
/// DLL Dynamic Distribution List
/// </summary>
public enum DLT { DL = 1, DDL = 2 }

/// <summary>
/// This Class will Hold all the information about DL or DDL 
/// and based on the DLT it will call the disignated function to return the list of email address
/// </summary>
public class DistributionList
{
	public DLT DType { get; set; }
	public string CN { get; set; }
	public string FILORDN { get; set; }
}


Then the code for every type of distribution list I hope that you will find a good use for that

/// <summary>
/// Get the List of A dynamic distribution Group in Key Value Pairs 
/// the key is the CN
/// the value is the filter for the membership of that group
/// </summary>
/// <returns>Dictionary of strings </returns>
public List<DistributionList> GetDynamicDistributionLists() 
{
	List<DistributionList> distrubutionLists = new List<DistributionList>();

	using (var group = new DirectoryEntry("GC://dc=xyz,dc=com"))
	{
		
		using (var searchRoot = new DirectoryEntry("GC://xxx.xxx.xxx.xxx/dc=xyz,dc=com"))
		using (var searcher = new DirectorySearcher(searchRoot, "(ObjectClass=msExchDynamicDistributionList)"))
		using (var results = searcher.FindAll())
		{
			foreach (SearchResult result in results)
			{
				if (result.Properties.Contains("cn") && result.Properties.Contains("msExchDynamicDLFilter"))
				{
					DistributionList dl = new DistributionList();
					
					dl.DType = DLT.DDL;
					dl.CN = result.Properties["cn"][0].ToString();
					dl.FILORDN = result.Properties["msExchDynamicDLFilter"][0].ToString();

					distrubutionLists.Add(dl); 
				}
			}
		}

	}

	return distrubutionLists;

}

/// <summary>
/// Get the List of A distribution Group in Key Value Pairs
/// the key is the CN
/// the value is the DN of the Group
/// </summary>
/// <returns>Dictionary of strings</returns>
public List<DistributionList> GetDistributionLists() 
{
	List<DistributionList> distrubutionLists = new List<DistributionList>();

	using (var group = new DirectoryEntry("GC://dc=xyz,dc=com"))
	{

		using (var searchRoot = new DirectoryEntry("GC://xxx.xxx.xxx.xxx/dc=xyz,dc=com"))
		using (var searcher = new DirectorySearcher(searchRoot, "(&(objectCategory=group)(!groupType:1.2.840.113556.1.4.803:=2147483648))"))
		using (var results = searcher.FindAll())
		{
			foreach (SearchResult result in results)
			{
				if (result.Properties.Contains("cn") && result.Properties.Contains("distinguishedName"))
				{
					DistributionList dl = new DistributionList();
					
					dl.DType = DLT.DL;
					dl.CN = result.Properties["cn"][0].ToString();
					dl.FILORDN = result.Properties["distinguishedName"][0].ToString();

					distrubutionLists.Add(dl);
				}
			}
		}

	}

	return distrubutionLists;
}

/// <summary>
/// List Distribution List Members
/// </summary>
/// <param name="dn"> The distinguishedName of the Group </param>
/// <returns>List of Email Addresses</returns>
public List<string> listDLMembers(string dn) 
{
	List<string> addresses = new List<string>();

	using (var group = new DirectoryEntry("GC://dc=xyz,dc=com"))
	{

		using (var searchRoot = new DirectoryEntry("GC://xxx.xxx.xxx.xxx/dc=xyz,dc=com"))
		using (var searcher = new DirectorySearcher(searchRoot, "(&(objectCategory=person)(|(objectClass=contact)(objectClass=user))(memberOf=" + dn + "))"))
		using (var results = searcher.FindAll())
		{
			foreach (SearchResult result in results)
			{
				if (result.Properties.Contains("mail"))
					addresses.Add(result.Properties["mail"][0].ToString());
			}
		}
	}
	return addresses;
}

/// <summary>
/// List Dynamic Distribution List Members
/// </summary>
/// <param name="filter"> Filter to be used in the LDAP Query</param>
/// <returns>List Of Email Addresses</returns>
public  List<string>  listDDLMembers(string filter) 
{
   // The filter is the value of the previous dictionary

	List<string> addresses = new List<string>();

	using (var group = new DirectoryEntry("GC://dc=xyz,dc=com"))
	{

		using (var searchRoot = new DirectoryEntry("GC://xxx.xxx.xxx.xxx/dc=xyz,dc=com"))
		using (var searcher = new DirectorySearcher(searchRoot, filter))
		using (var results = searcher.FindAll())
		{
			foreach (SearchResult result in results)
			{
				if (result.Properties.Contains("mail"))
					addresses.Add(result.Properties["mail"][0].ToString());
			}
		}
	}
	return addresses;
}
Posted in Programming, Technology and tagged , , , , , , , , . Bookmark the permalink. RSS feed for this post. Leave a trackback.

3 Responses to C# Get Exchange Distribution and Dynamic Distribution Lists and their members

  1. Pingback: C# how to pass a function with parameters to another function | Saddam Abu Ghaida

  2. Mark Cotgrove says:

    In your method listDDLMembers what would the ldap query look like? I’ve tried all kinds of examples with no success.

    Basically what I need is to determine if a user is in a particular dynamic distribution list, I have the distribution list attributes and can search on anything, I’m just having trouble cobbling together the actual query.

    Thanks.

    • the filter or DN will be the out put of

      1
      GetDynamicDistributionLists

      which I’m publishing using an object called

      1
      DistributionList

      and it will look something like this

      1
      (&(extensionAttribute3=xxx)(mailNickname=*)(!(name=SystemMailbox{*))(!(name=CAS_{*))(!(msExchRecipientTypeDetails=16777216))(!(msExchRecipientTypeDetails=536870912))(!(msExchRecipientTypeDetails=8388608)))

      i hope that helps

      Regards
      Saddam

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.

Swedish Greys - a WordPress theme from Nordic Themepark.