Click here to Skip to main content
15,166,980 members
Articles / Programming Languages / C#
Posted 25 Apr 2004


59 bookmarked

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
A Windows control for selecting objects in Active Directory.

Picker control test app


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


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;
    adh = new ADHelper();
    IDictionaryEnumerator enumerator = adh.Children.GetEnumerator();

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

        // specific object types
        if (!alExceptions.Contains(node.Text))
            childNode.ImageIndex =
            childNode.ImageIndex = 3; // computer image
    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);
        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;


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.


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


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


About the Author

Christian Merritt
Chief Technology Officer
Australia Australia
Living abroad and loving life.

Comments and Discussions

GeneralThe doubled child items [modified] Pin
psychic8628-Jul-08 22:23
Memberpsychic8628-Jul-08 22:23 
GeneralRe: The doubled child items Pin
gisli.gudmundsson15-Feb-10 12:38
Membergisli.gudmundsson15-Feb-10 12:38 
GeneralRe: The doubled child items Pin
gisli.gudmundsson15-Feb-10 12:57
Membergisli.gudmundsson15-Feb-10 12:57 
GeneralRe: The doubled child items Pin
gisli.gudmundsson15-Feb-10 14:02
Membergisli.gudmundsson15-Feb-10 14:02 
GeneralThank you very much Pin
lucastar17-Jan-08 19:42
Memberlucastar17-Jan-08 19:42 
GeneralRe: Thank you very much Pin
Christian Merritt24-Jan-08 4:05
MemberChristian Merritt24-Jan-08 4:05 
GeneralRe: Thank you very much Pin
lucastar5-Feb-08 7:31
Memberlucastar5-Feb-08 7:31 
GeneralThanks Pin
hkdl200027-Jun-07 9:46
Memberhkdl200027-Jun-07 9:46 
GeneralRe: Thanks Pin
Christian Merritt28-Jun-07 17:10
MemberChristian Merritt28-Jun-07 17:10 
GeneralOUs are doubled Pin
andrei_teodorescu8-Jul-06 17:05
Memberandrei_teodorescu8-Jul-06 17:05 
GeneralRe: OUs are doubled Pin
sebaj24-Feb-07 5:21
Membersebaj24-Feb-07 5:21 
GeneralNames ordered AD Pin
tysonx6-Jun-06 5:17
Membertysonx6-Jun-06 5:17 
GeneralRe: Names ordered AD Pin
Christian Merritt12-Jun-06 3:07
MemberChristian Merritt12-Jun-06 3:07 
QuestionHow can i get the selected object? Pin
rbmrf9-Mar-06 8:29
Memberrbmrf9-Mar-06 8:29 
AnswerRe: How can i get the selected object? Pin
Christian Merritt9-Mar-06 8:48
MemberChristian Merritt9-Mar-06 8:48 
AnswerRe: How can i get the selected object? Pin
Data-Base17-Jul-08 4:53
MemberData-Base17-Jul-08 4:53 
QuestionSorted Output? Pin
VBDelinquent19-Dec-05 4:41
MemberVBDelinquent19-Dec-05 4:41 
AnswerRe: Sorted Output? Pin
Christian Merritt22-Dec-05 8:00
MemberChristian Merritt22-Dec-05 8:00 
GeneralHelp Using in VS2005 Pin
astocks19-Oct-05 17:50
Memberastocks19-Oct-05 17:50 
GeneralRe: Help Using in VS2005 Pin
Christian Merritt20-Oct-05 2:27
MemberChristian Merritt20-Oct-05 2:27 
GeneralRe: Help Using in VS2005 Pin
astocks20-Oct-05 10:48
Memberastocks20-Oct-05 10:48 
GeneralRe: Help Using in VS2005 Pin
Christian Merritt20-Oct-05 12:40
MemberChristian Merritt20-Oct-05 12:40 
GeneralRe: Help Using in VS2005 Pin
astocks21-Oct-05 15:17
Memberastocks21-Oct-05 15:17 
QuestionSpecified domain does not exist Pin
Member 784426-Sep-05 0:58
MemberMember 784426-Sep-05 0:58 
AnswerRe: Specified domain does not exist Pin
Christian Merritt12-Sep-05 4:48
MemberChristian Merritt12-Sep-05 4:48 

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.