none
C# How to query "All Address Lists" and get list of users in each list. RRS feed

  • Question

  • Hi Folks,

    Just wondering if anyone can give some pointers on solving the following problem.

    Using an A/D query I would like to get the contents of the "All Address Lists" container for an exchange installation (It contains the All Contacts, All Groups etc. lists) as well as the "All System Address Lists" container.

    The user would select from one of these lists to see all of the members in that address list. Once I have the name of the address list I can just do a query on the ShowInAddressBook property and ObjectClass=Person to get a list of users.

    The problem I am having is how to get the contents of the Address Lists container (I can see it in Sites and Services), given that the LDAP query might be different for each exchange installation.

    Regards

    Andy

    Update: Using Exchange 2010 and C# Net 4.5.
    • Edited by AndyW2007 Tuesday, August 20, 2013 3:59 AM
    Tuesday, August 20, 2013 3:58 AM

Answers

  • You can only have one Exchange Org per forest and there is only one configuration partition per forest so something like this should work regardless of the OrgName and environment.

                String sqSearchQuery = "(&(CN=All Address Lists)(objectClass=addressBookContainer))";
    
                SearchResultCollection srSearchResults;
                // Connect to RootDSE
                DirectoryEntry RootDSE = new DirectoryEntry("LDAP://rootDSE");
    
                // Retrieve the Configuration Naming Context from RootDSE
                string configNC =
                  RootDSE.Properties["configurationNamingContext"].Value.ToString();
    
                // Connect to the Configuration Naming Context
                DirectoryEntry configSearchRoot = new DirectoryEntry("LDAP://" + configNC);
    
    
                DirectorySearcher dsDirectorySearcher = new DirectorySearcher(configSearchRoot);
                dsDirectorySearcher.SearchScope = SearchScope.Subtree;
                dsDirectorySearcher.PageSize = 1000;
                dsDirectorySearcher.Filter = sqSearchQuery;
                srSearchResults = dsDirectorySearcher.FindAll();
                foreach (SearchResult srSearchResult in srSearchResults)
                {
    
                    DirectoryEntry deADEntry = srSearchResult.GetDirectoryEntry();
                    Console.WriteLine(deADEntry.Name);
                }
    Cheers
    Glen
    • Marked as answer by AndyW2007 Wednesday, August 21, 2013 3:27 AM
    Tuesday, August 20, 2013 6:15 AM

All replies

  • You can only have one Exchange Org per forest and there is only one configuration partition per forest so something like this should work regardless of the OrgName and environment.

                String sqSearchQuery = "(&(CN=All Address Lists)(objectClass=addressBookContainer))";
    
                SearchResultCollection srSearchResults;
                // Connect to RootDSE
                DirectoryEntry RootDSE = new DirectoryEntry("LDAP://rootDSE");
    
                // Retrieve the Configuration Naming Context from RootDSE
                string configNC =
                  RootDSE.Properties["configurationNamingContext"].Value.ToString();
    
                // Connect to the Configuration Naming Context
                DirectoryEntry configSearchRoot = new DirectoryEntry("LDAP://" + configNC);
    
    
                DirectorySearcher dsDirectorySearcher = new DirectorySearcher(configSearchRoot);
                dsDirectorySearcher.SearchScope = SearchScope.Subtree;
                dsDirectorySearcher.PageSize = 1000;
                dsDirectorySearcher.Filter = sqSearchQuery;
                srSearchResults = dsDirectorySearcher.FindAll();
                foreach (SearchResult srSearchResult in srSearchResults)
                {
    
                    DirectoryEntry deADEntry = srSearchResult.GetDirectoryEntry();
                    Console.WriteLine(deADEntry.Name);
                }
    Cheers
    Glen
    • Marked as answer by AndyW2007 Wednesday, August 21, 2013 3:27 AM
    Tuesday, August 20, 2013 6:15 AM
  • Thanks for that,  I had done something similar but had not set the configurationNamingContext which is why I wasn't getting any results :)

    I wonder if EWS will ever have a GetAddressLists(AddressListType) style of API call.


    • Edited by AndyW2007 Wednesday, August 21, 2013 3:28 AM
    Wednesday, August 21, 2013 3:27 AM