943,998 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1424
  • C# RSS
Nov 6th, 2009
0

Active Directory Query and TreeView Help

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  1.  
  2. private void srchByID()
  3. {
  4. try
  5. {
  6. string Search = txtSearch.Text;
  7.  
  8. Root = Searcher.DE("contoso.com", "administrator", "system32!");
  9.  
  10. ADSearch = Searcher.DS(Root);
  11.  
  12. //ADSearch.Filter = "(&(objectClass=person)(" + SearchType + "=" + Search + "))";
  13. ADSearch.Filter = Searcher.Filter("Person", "SAMAccountName", Search);
  14.  
  15. SearchResultCollection results = ADSearch.FindAll();
  16.  
  17.  
  18. foreach (SearchResult result in results)
  19. {
  20.  
  21.  
  22.  
  23. Dictionary<string, ResultProperty>.KeyCollection kc = BuildResult(result).Keys; //This passes a Dictionary to 'kc'
  24. foreach (string key in kc) //iterate through dictionary and add keys to TreeView.
  25. {
  26. TreeNode node = default(TreeNode);
  27.  
  28. node = treeView1.Nodes.Add(key);
  29.  
  30. node.Tag = key;
  31.  
  32.  
  33. }
  34.  
  35. Dictionary<string, ResultProperty>.ValueCollection vc = BuildResult(result).Values;
  36. foreach (ResultProperty value in vc)
  37. {
  38.  
  39. // This is where I am needing help//
  40. //The code here should be a child node of the keys.
  41.  
  42. }
  43. ResultPropertyCollection rpc = result.Properties;
  44.  
  45. txtLogonName.Text = DATA.GetRSString("SAMAccountName", rpc);
  46. txtDisplayName.Text = DATA.GetRSString("DisplayName", rpc);
  47. txtEmail.Text = DATA.GetRSString("Mail", rpc);
  48. txtADsPath.Text = DATA.GetRSString("ADSPath", rpc);
  49. txtTelephoneNumber.Text = DATA.GetRSString("TelephoneNumber", rpc);
  50. txtSecretQuestion.Text = DATA.GetRSString("ExtensionAttribute1", rpc);
  51. txtTitle.Text = DATA.GetRSString("Title", rpc);
  52. txtDepartment.Text = DATA.GetRSString("Department", rpc);
  53. txtHomeDirectory.Text = DATA.GetRSString("HomeDirectory", rpc);
  54. txtBadPasswordCount.Text = DATA.GetRSString("BadPwdCount", rpc);
  55. txtCreatedOn.Text = DATA.GetRSString("WhenCreated", rpc);
  56. txtID.Text = DATA.GetRSString("Company", rpc);
  57.  
  58. dataGridView1.Rows.Add(DATA.GetRSString("SAMAccountName", rpc), DATA.GetRSString("Mail", rpc), DATA.GetRSString("DisplayName", rpc), DATA.GetRSString("ExtensionAttribute1", rpc), DATA.GetRSString("Company", rpc));
  59. }
  60.  
  61. if (results.Count == 0) MessageBox.Show("Unable to find: " + "" + Search + "", "Object Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
  62. else ADSearch.Dispose();
  63. }
  64. catch (COMException ex)
  65. {
  66. MessageBox.Show("" + ex.Message + "", "Query Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
  67. }
  68. }
Last edited by sanch01r; Nov 6th, 2009 at 9:54 am.
Similar Threads
Reputation Points: 10
Solved Threads: 3
Light Poster
sanch01r is offline Offline
30 posts
since Oct 2009
Nov 6th, 2009
0
Re: Active Directory Query and TreeView Help
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:

C# Syntax (Toggle Plain Text)
  1. TreeNode users = new TreeNode("Users");
  2. TreeNode groups = new TreeNode("Groups");
  3. TreeNode services = new TreeNode("Services");
  4. treeView1.Nodes.AddRange(new TreeNode[] { users, groups, services });
  5.  
  6. directoryEntry1.Path = @"WinNT://WORKGROUP/"+ Environment.MachineName;
  7.  
  8. foreach (System.DirectoryServices.DirectoryEntry child
  9. in directoryEntry1.Children)
  10. {
  11. TreeNode newNode = new TreeNode(child.Name);
  12. switch (child.SchemaClassName)
  13. {
  14. case "User":
  15. users.Nodes.Add(newNode);
  16. break;
  17. case "Group":
  18. groups.Nodes.Add(newNode);
  19. break;
  20. case "Service":
  21. services.Nodes.Add(newNode);
  22. break;
  23. }
  24. AddPathAndProperties(newNode, child);
  25.  
  26. }
  27.  
  28. private void AddPathAndProperties(TreeNode node, DirectoryEntry entry)
  29. {
  30. node.Nodes.Add(new TreeNode("Path: " + entry.Path));
  31. TreeNode propertyNode = new TreeNode("Properties");
  32. node.Nodes.Add(propertyNode);
  33. foreach (string propertyName in entry.Properties.PropertyNames)
  34. {
  35. string oneNode = propertyName + ": " +
  36. entry.Properties[propertyName][0].ToString();
  37. propertyNode.Nodes.Add(new TreeNode(oneNode));
  38. }
  39. }
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Nov 15th, 2009
0
Re: Active Directory Query and TreeView Help
I ended up figuring this one out! Thanks a lot.
Reputation Points: 10
Solved Threads: 3
Light Poster
sanch01r is offline Offline
30 posts
since Oct 2009
Nov 15th, 2009
0
Re: Active Directory Query and TreeView Help
Click to Expand / Collapse  Quote originally posted by sanch01r ...
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.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Check DataGridViewCheckBoxColumn
Next Thread in C# Forum Timeline: Reading txt file, casting to array, Display in listBox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC