Click here to Skip to main content
Click here to Skip to main content

Active Directory object picker control

, 16 May 2004
Rate this:
Please Sign up or sign in to vote.
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.

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).

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)

About the Author

Marc Merritt
Architect
United States United States
I live in southeastern Pennsylvania, USA with my lovely wife and two beautiful daughters. Life is good. My hobbies are motorcycles, motorcycles, and motorcycles.
 
I run a riders group called Twisties Motorcycle Club. If you're are a rider in the tri-state area, look us up! http://twistiesmc.com/
Follow on   Twitter

Comments and Discussions

 
GeneralThe doubled child items [modified] Pinmemberpsychic8628-Jul-08 21:23 
GeneralRe: The doubled child items Pinmembergisli.gudmundsson15-Feb-10 11:38 
GeneralRe: The doubled child items Pinmembergisli.gudmundsson15-Feb-10 11:57 
GeneralRe: The doubled child items Pinmembergisli.gudmundsson15-Feb-10 13:02 
GeneralThank you very much Pinmemberlucastar17-Jan-08 18:42 
GeneralRe: Thank you very much PinmemberMarc Merritt24-Jan-08 3:05 
GeneralRe: Thank you very much Pinmemberlucastar5-Feb-08 6:31 
GeneralThanks Pinmemberhkdl200027-Jun-07 8:46 
GeneralRe: Thanks PinmemberMarc Merritt28-Jun-07 16:10 
GeneralOUs are doubled Pinmemberandrei_teodorescu8-Jul-06 16:05 
GeneralRe: OUs are doubled Pinmembersebaj24-Feb-07 4:21 
GeneralNames ordered AD Pinmembertysonx6-Jun-06 4:17 
GeneralRe: Names ordered AD PinmemberMarc Merritt12-Jun-06 2:07 
QuestionHow can i get the selected object? Pinmemberrbmrf9-Mar-06 7:29 
AnswerRe: How can i get the selected object? PinmemberMarc Merritt9-Mar-06 7:48 
AnswerRe: How can i get the selected object? PinmemberData-Base17-Jul-08 3:53 
QuestionSorted Output? PinmemberVBDelinquent19-Dec-05 3:41 
AnswerRe: Sorted Output? PinmemberMarc Merritt22-Dec-05 7:00 
Hi,
 
You could sort the Hashtable keys by implementing something like this:
 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp01212002.asp
 
Hope this helps.
 
-Marc
GeneralHelp Using in VS2005 Pinmemberastocks19-Oct-05 16:50 
GeneralRe: Help Using in VS2005 PinmemberMarc Merritt20-Oct-05 1:27 
GeneralRe: Help Using in VS2005 Pinmemberastocks20-Oct-05 9:48 
GeneralRe: Help Using in VS2005 PinmemberMarc Merritt20-Oct-05 11:40 
GeneralRe: Help Using in VS2005 Pinmemberastocks21-Oct-05 14:17 
QuestionSpecified domain does not exist Pinmembergiles papworth5-Sep-05 23:58 
AnswerRe: Specified domain does not exist PinmemberMarc Merritt12-Sep-05 3:48 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 17 May 2004
Article Copyright 2004 by Marc Merritt
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid