read AD Tree and display in treeview

Posted on 2009-02-12
Last Modified: 2013-11-07
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...

            More OUs
            More Objects
        Location 3
            More OUs

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

Many thanks.
Question by:ptea
    LVL 3

    Expert Comment

    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.
    LVL 3

    Expert Comment

    Ooops typo .....By default there is no console*********
    LVL 1

    Accepted Solution

    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)
        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);
        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);
        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";
                case "computer":
                    tn.ImageUrl = "~/pics/computer.gif";
                case "user":
                    tn.ImageUrl = "~/pics/user.gif";
                    tn.ImageUrl = "~/pics/locked.gif";
        protected void TreeView1_Load(object sender, EventArgs e)

    Open in new window


    Author Closing Comment

    ... sounds interesting - will try it soon. many thanks for the proposed solution.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
    This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now