473,378 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Populate TreeView with Active Directory

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
Nov 20 '05 #1
3 10108
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.

"Jan Wrage" <ja*******@gmx.net> wrote in message
news:u2**************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #2
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
"Mike Caputo" <mi************@radarwire.com> schrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

"Jan Wrage" <ja*******@gmx.net> wrote in message
news:u2**************@TK2MSFTNGP12.phx.gbl...
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


Nov 20 '05 #3
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.

"Jan Wrage" <ja*******@gmx.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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
"Mike Caputo" <mi************@radarwire.com> schrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

"Jan Wrage" <ja*******@gmx.net> wrote in message
news:u2**************@TK2MSFTNGP12.phx.gbl...
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



Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: T.H.M | last post by:
This is the all code. Very simple and short. I need to populate a treeView in aspx page (web form) from XML file. I get en completion error:No overload for method 'TreeNode' takes '1' arguments ....
0
by: Ravi | last post by:
i tried with MS treeview control but i am unable to implement all the requirements.This requiement like when user login will generate the menu and display it. After that we need to Add or remove...
1
by: James | last post by:
I was wondering if there is a way to populate a drop down list with the active directory user? So far I have found the namespace System.DirectoryServices but I am not sure how to access the users...
4
by: Ian Powell | last post by:
Hi I've got objects in an sorted ArrayList like: P:\ P:\\DOCS P:\\i386 P:\\i386\ASMS P:\\i386\ASMS\1000 P:\\i386\ASMS\1000\MSFT
1
by: Phil Kelly | last post by:
Hi all! Can someone tell me how to use a treeview control to list contents of Active Directory. Ideally, I would like to list only users and contacts. I know that a long time ago, I saw a...
4
by: Tim Frawley | last post by:
I am working with some path strings that I pulled from Active Directory. I want to populate a TreeView with these OUs and have been beating my head agains the monitor trying to work it out. ...
6
by: Beginner | last post by:
Hi, I'm trying to populate a TreeView from an existing and filled in ListView (lvwBuild). lvwBuild is a basic two column listview which can have any number of rows. I would like the first...
0
by: xmail123 | last post by:
How to populate a treeview from a dataset I am very new to C#. I need to create a Windows form that will read a SQL table and populate a treeview. I can connect to the DB, create the...
0
by: MrColeyted | last post by:
I am designing an app that is similar to Windows Explorer. I have all working well. By clicking a directory in the treeview, the folders and files for that directory are displayed in the listview....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.