Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Active Directory structure in TreeView control (VB.Net)

Status
Not open for further replies.

Tomeczek

Programmer
Oct 25, 2012
40
US
I’m working on a VB.Net project in which I have to use TreeView control filled with the Active Directory structure. The AD is very big, with hundreds of OUs and subOUs. Because of the size of AD I can’t fill the TreeView with the whole AD structure “in one shot” because it would take too long.
What I want to do first is to show just the first level of OUs/Containers (just under the domain). That I did using the code:

Code:
Dim rootNode as TreeNode = Nothing

‘Creating the top node with domain name
TreeViewCtl.Nodes().Clear()
rootNode = TreeViewCtl.Nodes.Add(“Domain.Net”)

I find first level of OUs/Containers. These are returned using LDAP search (OneLevel) in SearchResultCollection variable. I now build the tree of the results under the domain in the following loop:

Code:
Dim searchResults As SearchResultCollection
...
For Each foundOU In searchResults
        ...
        newChild = New TreeNode(sOU)
        rootNode.Nodes.Add(newChild)
        ...
Next

That creates the view of the first level OUs.

Here’s what I want to do now, and don’t know how to do it:

I want to put the “+” in front of the OUs that have subOUs without adding nodes. I know which OUs have subOUs, just need to get this “+”.
Then, clicking on the “+”, I want to get next level of OUs and add child nodes “on the fly”. The LDAP search I can do, just need the TreeViewCtl part.

Now I need some suggestions from those of you who understand my goal and know how to accomplish it.

Thanks in advance!
 
Since you know what nodes need the plus sign, you can put a "dummy" node underneath those nodes to get the plus sign to show up. Then in the TreeView's BeforeExpand event, you can remove the dummy node, fetch the AD info and populate the node correctly. I also suggest giving the dummy nodes the same name (or tag), so you can check for its existence in the BeforeExpand event, and if it does not exist (i.e., the node has been expanded once before and already has the AD info), you can skip re-populating the AD info.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thank you jebenson for your response.

You gave me an idea, which I used: instead of putting "Dummy" node, I put the first subOU (if one exist) underneath. Then, while expanding, I repeat the same routine.

Works as I wanted: I'm adding child nodes "on the fly" when clicking on the "+" sign.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top