Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Writing a vb.net application that needs to display a list of Active Directory Dynamic Distribution Groups. The code below works when retrieving a list of groups - but returns nothing when I add the OU=DynamicDistributionGroups to the context definition. Can Dynamic Distribution Groups be obtained using the GroupPrincipal this way? Suggestions are welcome? Thanks

VB
Using ctx As New PrincipalContext 
  (ContextType.Domain, "MYLAN", "OU=DynamicDistributionGroups,OU=Email Groups,DC=mylan,DC=ac,DC=mycompany,DC=com")

   Dim pGroup As New GroupPrincipal(ctx)
   pGroup.Name = "*"
   Dim pSearcher As New PrincipalSearcher()
   pSearcher.QueryFilter = pGroup
   Dim results As PrincipalSearchResult(Of Principal) = pSearcher.FindAll()
   For Each p As Principal In results
       listGroup.Items.Add(p.ToString())
   Next
End Using
Posted

You should first try to get all groups including the Dynamic Distribution Groups. I believe (without having the possibility to test it here) that you need to remove both OU's from your definition.

Then, you can check for each group if it's a SecurityGroup. If not, it's a Dynamic Distribution Group...
VB
Using ctx As New PrincipalContext 
  (ContextType.Domain, "MYLAN", "DC=mylan,DC=ac,DC=mycompany,DC=com")
 
   Dim pGroup As New GroupPrincipal(ctx)
   pGroup.Name = "*"
   Dim pSearcher As New PrincipalSearcher()
   pSearcher.QueryFilter = pGroup
   Dim results As PrincipalSearchResult(Of Principal) = pSearcher.FindAll()
   For Each p As Principal In results
       Dim gp As GroupPrincipal = p
       If Not gp.IsSecurityGroup Then listGroup.Items.Add(p.ToString())
   Next
End Using

Disclaimer: untested code, no VS and no AD here...
 
Share this answer
 
Thanks for the proposed solution. I was however able to accomplish the task using DirectorySearcher rather than PrincipalContext as follows:

VB
Dim results As SearchResultCollection
Dim srch As New DirectorySearcher("LDAP://MYLAN/OU=DynamicDistributionGroups,OU=Email Groups,DC=mylan,DC=ac,DC=mycmpany,DC=com")
srch.Filter = "(objectClass=msExchDynamicDistributionList)"
srch.PropertiesToLoad.Add("displayName")
srch.PageSize = 1000
results = srch.FindAll()
For Each result As SearchResult In results
    Dim props As ResultPropertyCollection = result.Properties
    For Each propName As String In props.PropertyNames
        Dim groupName As String = props(propName)(0)
    Next
Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900