Active Directory Query and TreeView Help

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 28
Reputation: sanch01r is an unknown quantity at this point 
Solved Threads: 2
sanch01r sanch01r is offline Offline
Light Poster

Active Directory Query and TreeView Help

 
0
  #1
Nov 6th, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 964
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 206
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #2
Nov 6th, 2009
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:

  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 28
Reputation: sanch01r is an unknown quantity at this point 
Solved Threads: 2
sanch01r sanch01r is offline Offline
Light Poster
 
0
  #3
34 Days Ago
I ended up figuring this one out! Thanks a lot.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 964
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 206
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #4
33 Days Ago
Originally Posted by sanch01r View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC