Help with directorySearcher and Treeview

jagoh87

Member
Joined
Nov 21, 2006
Messages
8
Programming Experience
Beginner
I have the following code to list all the users in my AD tree and populate a treeview from it:

VB.NET:
Sub FillActiveDirectoryTreeView(ByRef tvw As TreeView, ByVal TopLevelDirEntriesArray As DirectoryEntry)

        Dim DirEntry As DirectoryEntry
        Dim Nd As TreeNode

        For Each DirEntry In TopLevelDirEntriesArray.Children
            Nd = New TreeNode(DirEntry.Name.Remove(0, 3))
            Nd.Tag = DirEntry
            tvw.Nodes.Add(Nd)
            FillADTreeNode(Nd)
        Next

    End Sub

    Sub FillADTreeNode(ByRef ADNode As TreeNode)
        Dim DirEntry As DirectoryEntry
        Dim MainEntry As DirectoryEntry = CType(ADNode.Tag, DirectoryEntry)
        Dim Nd As TreeNode
        For Each DirEntry In MainEntry.Children

            Nd = New TreeNode(DirEntry.Name.Remove(0, 3))
            Nd.Tag = DirEntry
            ADNode.Nodes.Add(Nd)
            FillADTreeNode(Nd)
            Nd.Tag = Nd.Text
        Next
    End Sub

And i have the code to list all the users samAccountNames:

VB.NET:
 Dim de As DirectoryEntry = New DirectoryEntry("LDAP://OU=xxxxxxx,DC=xxxxxxxx,DC=Com")
        Dim objSearcher As DirectorySearcher = New DirectorySearcher(de)
        objSearcher.Filter = "(objectClass=User)"
        Dim objresults As SearchResult
        For Each objresults In objSearcher.FindAll()
            objresults.GetDirectoryEntry().Properties("sAMAccountName").Value.ToString()
        Next

What i want to do is set the samAccountName value as the tag of corrensponding user node in the treeview

any help would be appreciated
 

jagoh87

Member
Joined
Nov 21, 2006
Messages
8
Programming Experience
Beginner
Got a work around im not sure if its the best way to do it but it works for now

VB.NET:
Sub FillActiveDirectoryTreeView(ByRef tvw As TreeView, ByVal TopLevelDirEntriesArray As DirectoryEntry)

        Dim DirEntry As DirectoryEntry
        Dim Nd As TreeNode

        For Each DirEntry In TopLevelDirEntriesArray.Children
            Nd = New TreeNode(DirEntry.Name.Remove(0, 3))
            Nd.Tag = DirEntry
            tvw.Nodes.Add(Nd)
            FillADTreeNode(Nd)
            Nd.Tag = Nothing
        Next

    End Sub

    Sub FillADTreeNode(ByRef ADNode As TreeNode)
        Dim DirEntry As DirectoryEntry
        Dim MainEntry As DirectoryEntry = CType(ADNode.Tag, DirectoryEntry)
        Dim Nd As TreeNode
        For Each DirEntry In MainEntry.Children
            Nd = New TreeNode(DirEntry.Name.Remove(0, 3))
            Nd.Tag = DirEntry
            ADNode.Nodes.Add(Nd)
            FillADTreeNode(Nd)
            Nd.Tag = Nd.Text
        Next


    End Sub

    
    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        If TreeView1.SelectedNode.Tag = Nothing Then
        Else
            Dim cnName As String = TreeView1.SelectedNode.Tag
            Dim objsearch As New DirectorySearcher
            Dim objresults As SearchResult
            objsearch.SearchRoot = New DirectoryEntry("LDAP://OU=xxxxxx,DC=xxxxxx,DC=Com")
            objsearch.Filter = "(&(objectClass=user) (cn=" & cnName & "))"
            objsearch.PropertiesToLoad.Add("cn")
            For Each objresults In objsearch.FindAll()
                txtSelected.Text = (objresults.GetDirectoryEntry().Properties("sAMAccountName").Value.ToString())
            Next
        End If
        
    End Sub
 
Top Bottom