Populate TreeView with Active Directory

  • Thread starter Thread starter Jan Wrage
  • Start date Start date
J

Jan Wrage

Hi!

I would like to implement a treeview in my existing application. It should
show my entire Active-Directory structure, i.e. all Groups, Containers and
OUs.

Could somebody help me with that. Im trying for about 3 hours but can't get
it to work.

Thank you!

Greetings
Jan Wrage
 
Well, I'm not exactly sure whether you're asking how to fill a TreeView in
general, or how to access the Active Directory API. I'm not very familiar
with using Active Directory, but you can read about it in MSDN. The objects
used to interface with AD are in the System.DirectoryServices namespace.
You can use a recursively-called procedure to fill the TreeView using
DirectoryEntry objects.

Sub FillActiveDirectoryTreeView (ByRef tvw as TreeView, _
TopLevelDirEntriesArray() as DirectoryEntry)

Dim DirEntry as DirectoryEntry
Dim Nd as TreeNode

For Each DirEntry in TopLevelDirEntriesArray
Nd = New TreeNode(DirEntry.Name)
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)
Nd.Tag = DirEntry
ADNode.Nodes.Add(Nd)
FillADTreeNode(Nd)
Next

End Sub

This is just an adapted version of a procedure I use all the time. What I
would also suggest (and which is also the way I do it) is to create your own
derived Node class that stores a Directory Entry in its Tag. That way, you
can write Constructors that save some of the code above and expose some of
the most frequently-used properties. Trust me, it's worth it.

Hope this helps,
Mike
--


Michael Caputo
Programmer/Database Administrator
Simon Economic Systems Ltd.
 
Hi Mike!

Thanks for your code!

Although I cant get it to work. Im not familiar with this TreeView-Control,
i just need to get my AD in there.

What mean this 'TopLevelDirEntriesArray()' ?

Could u please help me again?.

My goal is to provide the domain name to a function and then the TreeView
will be filled. But for timing reasons, it should fill the visible nodes
only, the rest before expanding.

Thank you very much!

Jan Wrage
 
TopLevelDirEntriesArray() is just an array of directory entries at the top
level of the domain. Instead, you could pass in the domain name (with
Provider), then Construct a DirectoryEntry and loop on the Children:

Sub FillADTreeView(ByRef tvw as TreeView, ByVal DomainName as String)
Dim DomainEntry as New DirectoryEntry(DomainName)
Dim Nd as TreeNode

Nd = New TreeNode(DomainEntry.Name)
Nd.Tag = DomainEntry
tvw.Nodes.Add(Nd)

End Sub

Then you can basically use the same procedures I sent you before, but fill
the Domain node itself rather than the TreeView. I don't think it will work
if you only fill the top-level nodes, because they won't have an Expand sign
if they have no child nodes.... so all the user will see is a bunch of empty
nodes. However, if you're really concerned about speed, then I suggest what
you do is fill the top two levels in the AD hierarchy (i.e.
Domain\Level1Dir1\Level2Dir1), and then you can fill the nodes one level
down each time a node is expanded. For example:

Domain
Level1Dir1
Level2Dir1
Level2Dir2
Level1Dir2
Level2Dir3

All nodes at Levels 1 and 2 will be filled initially. Then, for the Expand
event of Level 1 nodes, all of its Level 2 child nodes will be filled, when
Level 2 nodes expand, Level 3 nodes will be filled, and so on. It's the
same recursion, just done at a different time:

Prive Sub tvwActiveDirectory_BeforeExpand(sender as Object, e as
TreeViewEventArgs) _
Handles tvwActiveDirectory.BeforeExpand
Dim Nd as Node
For Each Nd in e.Node 'The Expanding Node
FillADTreeNode(Nd) 'The same procedure I posted before
Next
End Sub

So, when the user clicks on the Node for Level1Dir1, the Children for the
Level2Dir1 and Level2Dir2 Nodes will be filled.

Hope this helps,
Mike

--


Michael Caputo
Programmer/Database Administrator
Simon Economic Systems Ltd.
 
Back
Top