Click here to Skip to main content
15,884,739 members
Articles / Programming Languages / C#
Article

Active Directory object picker control

Rate me:
Please Sign up or sign in to vote.
4.45/5 (19 votes)
16 May 2004CPOL1 min read 221.6K   2.3K   59   46
A Windows control for selecting objects in Active Directory.

Picker control test app

Introduction

A basic Windows control library for selecting objects in your Active Directory.

Background

I've been involved in several Active Directory projects and frequently have had the need for a "picker" utility to select objects in the AD hierarchy. I decided to roll my own and encapsulate it in a Windows control library.

Sample Usage

Add the control to the list of Windows Forms controls in Visual Studio .NET, then drag the control onto a Windows Form. The control will automatically obtain your Active Directory root path and begin building a hierachal tree of the objects it finds.

C#
private void AddTreeNodes(TreeNode node)
{
    Cursor.Current = Cursors.WaitCursor;
    treeView1.BeginUpdate();
    adh = new ADHelper();
    adh.GetChildEntries((string)node.Tag);
    IDictionaryEnumerator enumerator = adh.Children.GetEnumerator();

    // append children
    while (enumerator.MoveNext())
    {
        TreeNode childNode = new TreeNode((string)enumerator.Key);
        childNode.Tag = enumerator.Value; // ADsPath
        node.Nodes.Add(childNode);


        // specific object types
        if (!alExceptions.Contains(node.Text))
            childNode.ImageIndex =
                SetImageIndex(enumerator.Key.ToString().Substring(0,2));
        else
            childNode.ImageIndex = 3; // computer image
    }
    treeView1.EndUpdate();
    Cursor.Current = Cursors.Default;
}

AddTreeNodes() takes only one parameter, the parent TreeNode object. It then proceeds to add every child object it finds. Similar to Microsoft's ADSI Edit tool, the control displays a plus sign to indicate if a node has one or more child nodes.

Returning the ADsPath

Each time a node is selected in the hierarchy, the ADsPath public property is updated to reflect the fully-qualified path to the object (i.e. LDAP://OU=DEV,OU=US,DC=CORPORATE,DC=MYDOMAIN,DC=COM).

C#
public void GetChildEntries(string adspath)
{
    if (adspath.Length > 0)
        entry = new DirectoryEntry(adspath);
    else
        entry = new DirectoryEntry();

    foreach (DirectoryEntry childEntry in entry.Children)
    {
        _htChildren.Add(childEntry.Name, childEntry.Path);
    }
}
private void treeView1_AfterSelect(object sender, 
      System.Windows.Forms.TreeViewEventArgs e)
{
    _adspath = (string)e.Node.Tag;
}

Compatibility

The control has been tested on Windows 2000/XP/2003. It should work for Windows 9x/NT with the Microsoft Active Directory Client Extensions installed.

History

  • Version 1.0 - 04.22.2004 - First release version.
  • Version 1.1 - 04.28.2004 - Added image support for object types.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Zambia Zambia
Living abroad and loving life.

Comments and Discussions

 
GeneralRe: Specified domain does not exist Pin
Dave Midgley5-Oct-05 4:41
Dave Midgley5-Oct-05 4:41 
GeneralRe: Specified domain does not exist Pin
Christian Merritt20-Oct-05 12:01
Christian Merritt20-Oct-05 12:01 
GeneralRe: Specified domain does not exist Pin
Stefan de Groot17-Apr-07 8:02
Stefan de Groot17-Apr-07 8:02 
GeneralDisplay Pin
netman5423-Sep-04 0:30
netman5423-Sep-04 0:30 
GeneralPlease help me!!!! Pin
bekunkun20-Sep-04 20:51
bekunkun20-Sep-04 20:51 
Generalcould not add the adpicker Pin
vcorn14-Jun-04 20:38
vcorn14-Jun-04 20:38 
GeneralProblems unzipping file Pin
lezop31-May-04 4:48
lezop31-May-04 4:48 
GeneralRe: Problems unzipping file Pin
Christian Merritt31-May-04 9:53
Christian Merritt31-May-04 9:53 
GeneralRe: Problems unzipping file Pin
daluu13-Mar-06 9:25
daluu13-Mar-06 9:25 
GeneralProblems Unzipping the two files Pin
emi_oasis26-May-04 9:26
emi_oasis26-May-04 9:26 
GeneralAD Picker Pin
gmchugh28-Apr-04 21:23
gmchugh28-Apr-04 21:23 
GeneralRe: AD Picker Pin
Christian Merritt29-Apr-04 3:38
Christian Merritt29-Apr-04 3:38 
GeneralRe: AD Picker Pin
Dmitry Gulin16-May-04 21:57
Dmitry Gulin16-May-04 21:57 
GeneralSuggestion for TreeNode Images Pin
Heath Stewart26-Apr-04 8:18
protectorHeath Stewart26-Apr-04 8:18 
GeneralRe: Suggestion for TreeNode Images Pin
olivierv27-Apr-04 0:20
olivierv27-Apr-04 0:20 
GeneralRe: Suggestion for TreeNode Images Pin
Christian Merritt27-Apr-04 2:16
Christian Merritt27-Apr-04 2:16 
GeneralRe: Suggestion for TreeNode Images Pin
Tomaž Štih18-May-04 3:06
Tomaž Štih18-May-04 3:06 
GeneralRe: Suggestion for TreeNode Images Pin
Heath Stewart18-May-04 3:39
protectorHeath Stewart18-May-04 3:39 
GeneralRe: Suggestion for TreeNode Images Pin
Christian Merritt18-May-04 3:56
Christian Merritt18-May-04 3:56 
GeneralRe: Suggestion for TreeNode Images Pin
mlorr7719-May-04 1:45
mlorr7719-May-04 1:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.