Enumerating a User's Distribution Lists
Enumerating a User's Distribution Lists
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
Visual Basic
' Enumerate Distribution Lists that a User is On ' This sample lists the groups that the user account is on. ' It does not expand the enumeration of nested groups. ' This sample requires a reference to the following libraries: ' Active DS Type Library (activeds.tlb) ' Microsoft ActiveX Data Objects 2.5 Library (msado15.tlb) Private Sub Main() Dim objRootDSE As IADs Dim objConnection As New ADODB.Connection Dim objCommand As New ADODB.Command Dim RS As ADODB.Recordset Dim RS2 As ADODB.Recordset Dim varDomainNC As Variant Dim strQuery As String Dim strAlias As String Dim v As Variant Dim x As Variant ' Change to the alias of the mailbox you are looking for. strAlias = "User1" ' Get the configuration naming context. Set objRootDSE = GetObject("LDAP://RootDSE") varDomainNC = objRootDSE.Get("defaultNamingContext") ' Open the connection. objConnection.Provider = "ADsDSOObject" objConnection.Open "ADs Provider" ' Build the query to find the user based on the alias. strQuery = "<LDAP://" & varDomainNC & ">;(mailNickName=" & strAlias & ");name,ADsPath,memberOf,primaryGroupID;subtree" objCommand.ActiveConnection = objConnection objCommand.CommandText = strQuery Set RS = objCommand.Execute ' If you get a result, then loop through the memberOf array. If Not RS.EOF Then RS.MoveFirst Debug.Print RS.Fields("name"), "is a member of these Groups:" ' You need to place the memberOf results in an array so that you can loop through it. v = RS.Fields("memberOf").Value For Each x In v Debug.Print x Next ' memberOf does not contain the primary group of which this mailbox is ' a part. You need to determine what that account is and list it. ' Search on objectclass = Group and then compare the primaryGroupID ' of the user with the primaryGroupToken of the group. When you find a ' match, stop. strQuery = "<LDAP://" & varDomainNC & ">;(objectclass=Group);distinguishedname,ADsPath,primaryGroupToken;subtree" objCommand.CommandText = strQuery Set RS2 = objCommand.Execute RS2.MoveFirst While Not RS2.EOF If RS2.Fields("primaryGroupToken") = RS.Fields("primaryGroupID") Then Debug.Print RS2.Fields("distinguishedname") RS2.MoveLast Else RS2.MoveNext End If Wend End If RS.Close RS2.Close objConnection.Close Set RS = Nothing Set RS2 = Nothing Set objCommand = Nothing Set objConnection = Nothing Set objRootDSE = Nothing End Sub
Send us your feedback about the Microsoft Exchange Server 2003 SDK.
Build: June 2007 (2007.618.1)
© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.