Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

read AD Tree and display in treeview

Posted on 2009-02-12
4
Medium Priority
?
700 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.
0
Comment
Question by:ptea
  • 2
4 Comments
 
LVL 3

Expert Comment

by:chrishudson123
ID: 23630709
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.
0
 
LVL 3

Expert Comment

by:chrishudson123
ID: 23630714
Ooops typo .....By default there is no console*********
0
 
LVL 1

Accepted Solution

by:
BBIT_DK earned 1500 total points
ID: 23720516
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

0
 

Author Closing Comment

by:ptea
ID: 31546452
... sounds interesting - will try it soon. many thanks for the proposed solution.
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

810 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