We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

read AD Tree and display in treeview

ptea
ptea asked
on
Medium Priority
718 Views
Last Modified: 2013-11-07
Hi
How can I read Active Directory Tree and display results in a Tree View? The main problem is reading the AD Tree, I think i can manage it then putting the result in treeview. Our Tree is like...

Production
    Country1
        Location1
            More OUs
            More Objects
        Location2
    Country2
        Location 3
            More OUs

... and I should get back till Locations only in the treeview.
Anyone has already done a similar problem?

Many thanks.
Comment
Watch Question

Chris HudsonCloud Security Architect
CERTIFIED EXPERT

Commented:
By default there is console whicjh will show the entire structure.I saw in some of the customer's enviornment ,they manually created custom MMC with all of their Domain info.
Chris HudsonCloud Security Architect
CERTIFIED EXPERT

Commented:
Ooops typo .....By default there is no console*********
Commented:
Hi
I have done a small usercontrol for a customer, that enumerates their AD from a specific OU and down.
Maybe you can use the code.

If you paste it into a usercontrol, that has a treeview, named TreeView1 you should be good to go.
Then you need to do some impersonation either in code, or through web.config, to make sure you are allowed to query the AD.

Also you should put in the correct path to the OU that you need access to.

The pictures are just some simple people and folder pics.

Best regards
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.DirectoryServices;
 
public partial class adtree : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            fillTreeView(TreeView1);
    }
 
    private void fillTreeView(TreeView trv)
    {
        DirectorySearcher ds = new DirectorySearcher();
        DirectoryEntry entry = new DirectoryEntry("LDAP://OU=SOMEOU,DC=SOMEDC,DC=SOMEDCDOMAIN");
        SearchResultCollection src;
 
        ds.SearchRoot = entry;
        
        src = ds.FindAll();
        
 
        TreeNode tnTop = new TreeNode();
        tnTop.Text = "  " + entry.Name.Substring(3);
        tnTop.ImageUrl = "~/pics/folder.gif";
 
        IEnumerator entrEnum = entry.Children.GetEnumerator();
 
 
        while (entrEnum.MoveNext())
        {
            TreeNode tn = new TreeNode();
            DirectoryEntry drCurrent = (DirectoryEntry)entrEnum.Current;
            tn.Text = drCurrent.Name;
 
            CreateChildren(tn, drCurrent);
 
            formatDirEntry(drCurrent, tn);
            tnTop.ChildNodes.Add(tn);
            
        }
        trv.Nodes.Add(tnTop);
    }
 
    private void CreateChildren(TreeNode tn, DirectoryEntry de)
    {
        foreach (DirectoryEntry deCurr in de.Children)
        {
            TreeNode tnNew = new TreeNode(deCurr.Name);
            CreateChildren(tnNew, deCurr);
 
            formatDirEntry(deCurr, tnNew);
            tn.ChildNodes.Add(tnNew);
        }
    }
 
    private void formatDirEntry(DirectoryEntry de, TreeNode tn)
    {
        tn.Text = "  " + tn.Text.Substring(3);
        tn.NavigateUrl = "default.aspx?path=" + de.Path;
 
        switch (de.SchemaEntry.Name)
        {
            case "organizationalUnit":
                tn.ImageUrl = "~/pics/folder.gif";
            break;
 
            case "computer":
                tn.ImageUrl = "~/pics/computer.gif";
            break;
 
            case "user":
                tn.ImageUrl = "~/pics/user.gif";
            break;
            
            default:
                tn.ImageUrl = "~/pics/locked.gif";
            break;
        }
    }
 
    protected void TreeView1_Load(object sender, EventArgs e)
    {
        ((TreeView)sender).Nodes[0].Expand();
    }
}

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
... sounds interesting - will try it soon. many thanks for the proposed solution.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.