0
Reply

Active Directory Browser Interface

Qaisar

Qaisar

Sep 25 2008 10:30 AM
5.3k

Hi

I am trying to create an interface that would allow the Active Directory Admins to modify the attribute values of AD Objects. I got all of the code for this application from the book "Pro.NET Directory Services Programming" (with some modification). I am at a point where it is supposed to build a list of the domain with all child objects underneath. When I debug the application it populates the list box with top level domain and only the first level child object. According to the book it is supposed to have "+" sign next to each child obect which can be expanded by double clicking, but that is what is not happening. Any help is appreciated.

Form1.cs

using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.DirectoryServices; using System.Reflection; namespace WindowsApplication1 { public partial class Form1 : Form { private DirectoryUtil util; public Form1() { InitializeComponent(); util = DirectoryUtil.Instance; util.OnBind += new DirectoryUtilEventHandler(RefreshedDirectoryHandler); InitTree(); } private void RefreshedDirectoryHandler(object sender, string path) { this.lblPath.Text = path; this.InitTree(); } private void InitTree() { this.tvwDirectory.Nodes.Clear(); this.tvwDirectory.Nodes.Add(new TreeNode(util.RootContainer.Name, 0, 0)); this.tvwDirectory.Nodes[0].Tag = new TreeNodeInfo(util.Path, true); int index = 0; foreach (DirectoryEntry child in util.SharedEntry.Children) { index = this.tvwDirectory.Nodes[0].Nodes.Add(new TreeNode(child.Name, 3, 4)); this.tvwDirectory.Nodes[0].Nodes[index].Tag = new TreeNodeInfo(child.Path, false); } this.tvwDirectory.ExpandAll(); } private void tvwDirectory_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e) { this.lstProperties.Items.Clear(); TreeNodeInfo nodeInfo = (TreeNodeInfo)e.Node.Tag; int index = 0; util.SharedEntry.Path = nodeInfo.Path; util.SharedEntry.RefreshCache(); this.lblPath.Text = nodeInfo.Path; if (!nodeInfo.IsLoaded) { try { foreach (DirectoryEntry child in util.SharedEntry.Children) { index = e.Node.Nodes.Add(new TreeNode(child.Name, 3, 4)); e.Node.Nodes[index].Tag = new TreeNodeInfo(child.Path, false); } nodeInfo.IsLoaded = true; e.Node.Tag = nodeInfo; this.tvwDirectory.SelectedNode.Expand(); } catch (Exception ex) { //Console.Write(e.Message); } } } } }

 DirectoryUtil.cs

using System; using System.Collections.Generic; using System.Text; using System.DirectoryServices; namespace WindowsApplication1 { public delegate void DirectoryUtilEventHandler(object sender, string path); public class DirectoryUtil { private static DirectoryUtil _direcoryUtil; private static DirectoryEntry _rootContainer; private static DirectoryEntry _sharedDirectorEntry; private string _path = null; private static object _lockObject = "For locking"; public event DirectoryUtilEventHandler OnBind; private DirectoryUtil() { _rootContainer = new DirectoryEntry(); _sharedDirectorEntry = new DirectoryEntry(); } public static DirectoryUtil Instance { get { lock (_lockObject) { if (_direcoryUtil == null) _direcoryUtil = new DirectoryUtil(); } return _direcoryUtil; } } private void RefreshDirectoryStructure() { _rootContainer.Path = _path; _rootContainer.RefreshCache(); _sharedDirectorEntry.Path = _path; _sharedDirectorEntry.RefreshCache(); if (OnBind != null) OnBind(this, _path); } public void Bind() { RefreshDirectoryStructure(); } public string Path { get { return _path; } set { _path = value; RefreshDirectoryStructure(); } } public DirectoryEntry SharedEntry { get { return _sharedDirectorEntry; } } public DirectoryEntry RootContainer { get { return _rootContainer; } } } }

TreeNodeInfo.cs

using System; using System.Collections.Generic; using System.Text; namespace WindowsApplication1 { class TreeNodeInfo { private bool _isLoaded; private string _ldapPath; public TreeNodeInfo(string Path, bool isLoaded) { _ldapPath = Path; _isLoaded = isLoaded; } public string Path { get { return _ldapPath; } } public bool IsLoaded { get { return _isLoaded; } set { _isLoaded = value; } } } }

Program.cs

using System; using System.Collections.Generic; using System.Windows.Forms; namespace WindowsApplication1 { static class Program { ///

/// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }