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

Dynamically browse Active Directory Objects

By , 1 Feb 2004
 

Sample Image - SADB.gif

Introduction

This guide shows how to create an application in C#, for browsing Active Directory objects. On the left navigate objects/containers with an TreeView control. Only AD root will be read in, after successful authentication. The "children" of the containers will be dynamically loaded after the user clicks on an object. If no "children" exists no, action will be performed. Attributes and their values, of marked containers on the TreeView control, will be displayed on the right ListView control. Both controls a separated with a splitter control. On start an authentication dialog asks user for domain, username, and password.

Ok let´s start!

  1. Create an empty Windows Forms project 
    1. Add the System.DirectoryServices.dll reference to your project
    2. Dont forget to include in the code : using System.DirectoryServices
  2. In your main form drag a panel from your toolbox on the form
    1. Set the panel dock property to fill
  3. Drag an TreeView control on the panel, name it ctr_tree
    1. Set the Treeview dock property to left
  4. Add a splitter control on the panel
    1. Set the splitter dock property to left
  5. Add a ListView Element on the panel, name it ctr_list
    1. Set the ListView property to dock fill
    2. Set the view property to details
  6. Add an EventListener --> AfterSelect to your TreeView Control
    1. Declare 2 global variables as private:private DirectoryEntry Base; private string[] str;
    2. In the EventListener of the TreeView control add following code:
    //Fill the TreeView dynamic after Click
    if( e.Node.Nodes.Count == 0 ){
    
      DirectoryEntry parent = (DirectoryEntry)e.Node.Tag;
                    
        if(parent != null){
                        
            if(parent.Children != null){
                            
        foreach(DirectoryEntry Iter in parent.Children){
    
            TreeNode childNode = e.Node.Nodes.Add(Iter.Name);
            childNode.Tag = Iter;
    
                            }
                            
                        }
                    }
                }
    
        //Fill the ListView Element
    try{
        
    DirectoryEntry list =(DirectoryEntry)e.Node.Tag;
    
        if(list!=null){
                    
               ctr_list.Clear();
            
        //Add some information to ListView ELement
        ctr_list.Columns.Add("Attribute",90,HorizontalAlignment.Left);
        ctr_list.Columns.Add("Value",350,HorizontalAlignment.Left);
    
    
    foreach(object listIter in list.Properties.PropertyNames){
    
        foreach(Object Iter in list.Properties[listIter.ToString()]){
        System.Windows.Forms.ListViewItem item = 
         new System.Windows.Forms.ListViewItem(listIter.ToString(),0);
    
    
        item.SubItems.Add(Iter.ToString());
        ctr_list.Items.AddRange( new ListViewItem[] {item});
    
                                
                            }
    
                        }
                    }
                }
    catch(System.Exception ex){
                    
        MessageBox.Show(ex.Message);
                }
    
  7. Now you can either connect to AD using a dialog which is asking for domain, username, passw. Or connect statically using hardcoded information in the DirectoryEntry "Connection string". In this example I'm using a connection dialog which is returning an string array of : domain, username, password.
  8. Now all we need is an Connect(string[] temp) method. Simply copy the method from the code snippet below and add it to your form:
    private void Connect(string[] temp){
            
        //Get the strings from Array
        str=temp;
    
        //Pass Connet info to DirextoryEntry object:
    Base = new DirectoryEntry("LDAP://"+str.GetValue(0).ToString(),
     str.GetValue(1).ToString(),str.GetValue(2).ToString());
                
        //Read the root:
        if(Base != null){
                        
        ctr_tree.Nodes.Clear();
        ctr_tree.BeginUpdate();
    
        TreeNode childNode = ctr_tree.Nodes.Add(Base.Name);
        childNode.Tag = Base;
                        
                        
        try{
                        
        foreach(DirectoryEntry rootIter in Base.Children){
                            
        TreeNode RootNode = childNode.Nodes.Add(rootIter.Name);
        RootNode.Tag = rootIter;
                                
                        }    
                    }
        finally{
                        
        childNode.Expand();
        ctr_tree.EndUpdate();
                    
                    }
                        
    
                }
    
            }
  9. Ok lets see how we wire everything together. Let´s take a look at the main:
    [STAThread]
    static void Main(){
    
        FrmConnect con = new FrmConnect();
    
        if(con.ShowDialog() == DialogResult.OK){
    
        //lets capture our User & Passw:
        string[] temp=con.returnResults();
                    
        //if User&Pass are submitted launch main
        Form1 frm = new Form1();
                    
        //Pass User&Passw to Connect method
        frm.Connect(temp);
        Application.Run(frm);
                }
            }
    

    Ok first we create an instance of the connection dialog before we launch the main frame. After domain, user, passw are submitted we connect and launch the main frame.

Conclusion

That's it!

License

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

About the Author

Sibster
Web Developer
Germany Germany
Member
Iam a student who started developing in C then C++ ( QT/Crossplatfrom ) and now C#.
Besides my studies, I am working at a german Bank in Frankfurt / Main, concerning Active Directory & Database applications.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionquery regarding codememberVamshi Mekala13 Feb '13 - 19:56 
GeneralMy vote of 4memberSavindraSingh30 Jan '13 - 2:56 
Generalexcellentmembersypatil28 Jul '09 - 8:04 
Questionin web applicationmemberomer nauman16 Jul '08 - 23:35 
AnswerRe: in web applicationmemberSibster17 Jul '08 - 4:24 
Generalawesome.memberomer nauman16 Jul '08 - 1:26 
Question? Can I access LDAP of Solaris environment from my WindowsmemberSuranjan Nandi9 Mar '06 - 3:17 
AnswerRe: ? Can I access LDAP of Solaris environment from my WindowsmemberSibster9 Mar '06 - 4:14 
GeneralLooks wonderful...memberfuzzylintman4 Feb '04 - 4:33 
GeneralRe: Looks wonderful...memberSibster4 Feb '04 - 22:10 
GeneralRe: Looks wonderful...sussAnonymous23 Aug '04 - 21:44 
GeneralRe: Looks wonderful...memberSibster25 Aug '04 - 2:08 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 2 Feb 2004
Article Copyright 2004 by Sibster
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid