Hey guys, I am writing an Active Directory Tool and I created a Dictionary<> that iterates through all of the available attributes from the LDAP server. The Keys are the names of the attributes (displayname, samaccountname, gn, sn, etc), the TValues are the associated values to the keys. I am able to add the Keys to a TreeView, but I need help adding all of the values as child nodes under it's appropriate Node. Any help would be greatly appreciated.

Here is the code:

private void srchByID()
                string Search = txtSearch.Text;
                Root = Searcher.DE("contoso.com", "administrator", "system32!");

                ADSearch = Searcher.DS(Root);

                //ADSearch.Filter = "(&(objectClass=person)(" + SearchType + "=" + Search + "))";
                ADSearch.Filter = Searcher.Filter("Person", "SAMAccountName", Search);       
                SearchResultCollection results = ADSearch.FindAll();

                foreach (SearchResult result in results)

                    Dictionary<string, ResultProperty>.KeyCollection kc = BuildResult(result).Keys; //This passes a Dictionary to 'kc'
                    foreach (string key in kc) //iterate through dictionary and add keys to TreeView.
                        TreeNode node = default(TreeNode);

                        node = treeView1.Nodes.Add(key);

                        node.Tag = key;


                    Dictionary<string, ResultProperty>.ValueCollection vc = BuildResult(result).Values;
                    foreach (ResultProperty value in vc)
                        // This is where I am needing help//
                       //The code here should be a child node of the keys.

                    ResultPropertyCollection rpc = result.Properties; 

                    txtLogonName.Text = DATA.GetRSString("SAMAccountName", rpc);
                    txtDisplayName.Text = DATA.GetRSString("DisplayName", rpc);
                    txtEmail.Text = DATA.GetRSString("Mail", rpc);
                    txtADsPath.Text = DATA.GetRSString("ADSPath", rpc);
                    txtTelephoneNumber.Text = DATA.GetRSString("TelephoneNumber", rpc);
                    txtSecretQuestion.Text = DATA.GetRSString("ExtensionAttribute1", rpc);
                    txtTitle.Text = DATA.GetRSString("Title", rpc);
                    txtDepartment.Text = DATA.GetRSString("Department", rpc);
                    txtHomeDirectory.Text = DATA.GetRSString("HomeDirectory", rpc);
                    txtBadPasswordCount.Text = DATA.GetRSString("BadPwdCount", rpc);
                    txtCreatedOn.Text = DATA.GetRSString("WhenCreated", rpc);
                    txtID.Text = DATA.GetRSString("Company", rpc);

                    dataGridView1.Rows.Add(DATA.GetRSString("SAMAccountName", rpc), DATA.GetRSString("Mail", rpc), DATA.GetRSString("DisplayName", rpc), DATA.GetRSString("ExtensionAttribute1", rpc), DATA.GetRSString("Company", rpc));

                if (results.Count == 0) MessageBox.Show("Unable to find: " + "" + Search + "", "Object Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else ADSearch.Dispose();
            catch (COMException ex)
                MessageBox.Show("" + ex.Message + "", "Query Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);

Recommended Answers

All 3 Replies

I don't have a LDAP server to play with. Here is an example of how to populate the child nodes using DirectoryEntry component that might help you:

TreeNode users = new TreeNode("Users");
            TreeNode groups = new TreeNode("Groups");
            TreeNode services = new TreeNode("Services");
            treeView1.Nodes.AddRange(new TreeNode[] { users, groups, services });

            directoryEntry1.Path = @"WinNT://WORKGROUP/"+ Environment.MachineName;

            foreach (System.DirectoryServices.DirectoryEntry child
               in directoryEntry1.Children)
                TreeNode newNode = new TreeNode(child.Name);
                switch (child.SchemaClassName)
                    case "User":
                    case "Group":
                    case "Service":
                AddPathAndProperties(newNode, child);


        private void AddPathAndProperties(TreeNode node, DirectoryEntry entry)
            node.Nodes.Add(new TreeNode("Path: " + entry.Path));
            TreeNode propertyNode = new TreeNode("Properties");
            foreach (string propertyName in entry.Properties.PropertyNames)
                string oneNode = propertyName + ": " +
                propertyNode.Nodes.Add(new TreeNode(oneNode));

I ended up figuring this one out! Thanks a lot.

I ended up figuring this one out! Thanks a lot.

That's great! Can you share your solution so that I and others might benefit? Please mark as solved also.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.