| | |
Active Directory Query and TreeView Help
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 28
Reputation:
Solved Threads: 2
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:
Here is the code:
C# Syntax (Toggle Plain Text)
private void srchByID() { try { 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); } }
Last edited by sanch01r; Nov 6th, 2009 at 9:54 am.
•
•
Join Date: Jul 2009
Posts: 956
Reputation:
Solved Threads: 198
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: C# Syntax (Toggle Plain Text)
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": users.Nodes.Add(newNode); break; case "Group": groups.Nodes.Add(newNode); break; case "Service": services.Nodes.Add(newNode); break; } AddPathAndProperties(newNode, child); } private void AddPathAndProperties(TreeNode node, DirectoryEntry entry) { node.Nodes.Add(new TreeNode("Path: " + entry.Path)); TreeNode propertyNode = new TreeNode("Properties"); node.Nodes.Add(propertyNode); foreach (string propertyName in entry.Properties.PropertyNames) { string oneNode = propertyName + ": " + entry.Properties[propertyName][0].ToString(); propertyNode.Nodes.Add(new TreeNode(oneNode)); } }
![]() |
Similar Threads
- Active Directory Hierarchy to TreeView - HELP (C#)
- ASP Active Directory (ASP.NET)
- e-Directory vs. Active Directory (Novell)
- try to access Active Directory in .NET -->system.runtime.interopservices.comexception (VB.NET)
- Integrating with Active Directory (OS X)
- Active Directory (Windows NT / 2000 / XP)
Other Threads in the C# Forum
- Previous Thread: Check DataGridViewCheckBoxColumn
- Next Thread: get data from database in dropdown list
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





