Get LDAP Member Details in vb.net

Getting all the details on all members……..if it helps you do let me know. .NET 2.0 or above.
 
Public Function GetDistributionListMembers(ByVal ldapQuery As String) As GenericCollection(Of Contact)
        Dim contacts As GenericCollection(Of Contact) = Nothing
        Dim StrCN As String
        StrCN = ldapQuery
        Try
            ‘ replace \ with \/ for escape sequence.
            ldapQuery = ldapQuery.Replace("/", "\/")
            Dim directory_entry As DirectoryEntry = New DirectoryEntry(Constants.LDAP_QUERY)
            Dim results As SearchResultCollection
            Dim searcher As DirectorySearcher = New DirectorySearcher(directory_entry)
            searcher.Filter = String.Format("(& (objectClass=group)(cn={0}))", StrCN)
            searcher.PropertiesToLoad.Add("ADsPath")
            searcher.SearchScope = System.DirectoryServices.SearchScope.Subtree
            results = searcher.FindAll()
            Dim resultEntry As SearchResult
            contacts = New GenericCollection(Of Contact)
            For Each resultEntry In results
                Dim propertyCollection As ResultPropertyCollection = resultEntry.Properties
                Dim key As String
                For Each key In propertyCollection.PropertyNames
                    Dim values As Object
                    For Each values In propertyCollection(key)
                        If (UCase(key) = UCase(Constants.ADSPATH)) And (CheckIfDistributionList(values.ToString())) Then
                            GetMemberDetails(ldapQuery, StrCN, contacts)
                        End If
                    Next
                Next
            Next
        Catch ex As Exception
            Log.LogError("Error in Getting Members for Distribution list: " + ldapQuery.Substring(10, ldapQuery.IndexOf(",OU=") – 10), CommandOptions)
        End Try
        Return contacts
    End Function

Public Function GetMemberDetails(ByVal ldapQuery As String, ByVal strCN As String, ByRef contacts As GenericCollection(Of Contact)) As GenericCollection(Of Contact)

        Try
            ‘Search with in a domain for Distribution lists and members
            Dim dmSearch As DirectorySearcher
            Dim dmResult As SearchResult
            Dim dEntry, dmDir, dmUser As DirectoryEntry
            Dim dmEntry, dmList As Object
            Dim dmEntries As IEnumerable

            ‘ setup a directory searcher using a starting Domain and OU
            dEntry = New DirectoryEntry(Constants.LDAP_QUERY_DOMAIN)
            dmSearch = New DirectoryServices.DirectorySearcher(dEntry)

            ‘ switch the filter for Groups (same Domain/OU)
            dmSearch.Filter = "(&(objectClass=group)(name=" & strCN & "))"

            For Each dmResult In dmSearch.FindAll()
                dmDir = dmResult.GetDirectoryEntry
                ‘ get the list of Users from the Group
                dmList = dmDir.Invoke("Members")
                dmEntries = CType(dmList, IEnumerable)
                For Each dmEntry In dmEntries
                    dmUser = New DirectoryEntry(dmEntry)

                    ‘If this is a group – loop again
                    If CheckIfDistributionList(dmUser.Path) = True Then

                        Dim olEntry, olDir, olUser As DirectoryEntry
                        Dim olEntryObj, olListObj As Object
                        Dim olEntries As IEnumerable
                        Dim olSearch As DirectorySearcher
                        olEntry = New DirectoryEntry(dmUser.Path)
                        olSearch = New DirectoryServices.DirectorySearcher(olEntry)
                        ‘ switch the filter for Groups (same Domain/OU)
                        Dim result1 As SearchResult
                        For Each result1 In olSearch.FindAll()
                            olDir = result1.GetDirectoryEntry
                            ‘ get the list of Users from the Group
                            olListObj = olDir.Invoke("Members")
                            olEntries = CType(olListObj, IEnumerable)
                            For Each olEntryObj In olEntries
                                olUser = New DirectoryEntry(olEntryObj)
                                If CheckIfDistributionList(olUser.Path) = False Then
                                    If CkeckAccountsDL(olUser.Path) = True Then
                                        AddToContactsList(olUser, contacts)
                                    End If
                                End If
                            Next
                        Next

                    Else   ‘If this is not a group
                        If CkeckAccountsDL(dmUser.Path) = True Then
                            AddToContactsList(dmUser, contacts)
                        End If

                    End If
                Next
            Next
        Catch ex As Exception
            Throw
        End Try
        Return contacts
    End Function


Join me on Facebook

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: