How can I enumerate through OU's for nodes and add them to an existing treeview root node?
-
Hello, I am populating the root nodes of the treenode1 via the formload() function, I guess you could say they are hard coded. After that I want to select a root node (which will be a domain) and enumerate the OUs of that domain and then make the outputted (via the code below) sub nodes of the root node (domain) that is already listed. The function below makes the output, root nodes under the formload() created root nodes instead of subnodes under the formload() created root nodes, how can I make them subnodes of the original formload() root nodes? Sorry if this is written in a confusing manner, I've tried to reread it and make it less confusing amidst my own confusion.
private void searchForOus(string ouPath) { DirectoryEntry ADentry = new DirectoryEntry(ouPath, @"domain\\user", "password", AuthenticationTypes.Secure); DirectorySearcher Searcher = new DirectorySearcher(ADentry); Searcher.Filter = ("(objectClass=organizationalUnit)"); foreach (DirectoryEntry entry in Searcher.SearchRoot.Children) { Console.WriteLine(entry.Path); if (entry.Path == "LDAP://OU=Core Member Servers,dc=XXX,dc=XXX,dc=XX,dc=XXX") { if (ShouldAddNode(entry.SchemaClassName)) { treeView1.Nodes.Add(GetChildNode(entry)); } } } private TreeNode GetChildNode(DirectoryEntry entry) { TreeNode node = new TreeNode(entry.Name.Substring(entry.Name.IndexOf('=') + 1)); foreach (DirectoryEntry childEntry in entry.Children) { if (ShouldAddNode(childEntry.SchemaClassName)) { node.Nodes.Add(GetChildNode(childEntry)); } } return node; } private bool ShouldAddNode(string schemaClass) { bool returnValue = false; if (schemaClass == "organizationalUnit") { returnValue = true; } return returnValue; }
-
Hello, I am populating the root nodes of the treenode1 via the formload() function, I guess you could say they are hard coded. After that I want to select a root node (which will be a domain) and enumerate the OUs of that domain and then make the outputted (via the code below) sub nodes of the root node (domain) that is already listed. The function below makes the output, root nodes under the formload() created root nodes instead of subnodes under the formload() created root nodes, how can I make them subnodes of the original formload() root nodes? Sorry if this is written in a confusing manner, I've tried to reread it and make it less confusing amidst my own confusion.
private void searchForOus(string ouPath) { DirectoryEntry ADentry = new DirectoryEntry(ouPath, @"domain\\user", "password", AuthenticationTypes.Secure); DirectorySearcher Searcher = new DirectorySearcher(ADentry); Searcher.Filter = ("(objectClass=organizationalUnit)"); foreach (DirectoryEntry entry in Searcher.SearchRoot.Children) { Console.WriteLine(entry.Path); if (entry.Path == "LDAP://OU=Core Member Servers,dc=XXX,dc=XXX,dc=XX,dc=XXX") { if (ShouldAddNode(entry.SchemaClassName)) { treeView1.Nodes.Add(GetChildNode(entry)); } } } private TreeNode GetChildNode(DirectoryEntry entry) { TreeNode node = new TreeNode(entry.Name.Substring(entry.Name.IndexOf('=') + 1)); foreach (DirectoryEntry childEntry in entry.Children) { if (ShouldAddNode(childEntry.SchemaClassName)) { node.Nodes.Add(GetChildNode(childEntry)); } } return node; } private bool ShouldAddNode(string schemaClass) { bool returnValue = false; if (schemaClass == "organizationalUnit") { returnValue = true; } return returnValue; }
I don't see you creating the root node that you want to add sub-nodes to and using that outside the recursive call. See if this gives you some ideas: 1. take/create the selected root node of the file/directory structure 2. create the root level TreeNode that maps to root #1 e. recursively iterate the sub-nodes of #1, selecting by your 'should add' function
DirectoryEntry ADentry = new DirectoryEntry(ouPath, @"domain\user", "password", AuthenticationTypes.Secure);
DirectorySearcher Searcher = new DirectorySearcher(ADentry);
Searcher.Filter = "@(objectClass=organizationalUnit)";string EntryPath = @"LDAP:/OU=Core Member Servers,dc=XXX,dc=XXX,dc=XX,dc=XXX");
string schemaClass = @"organizationalUnit";
TreeNode WorkingTreeNode;
private void searchForOus(string ouPath)
{
foreach (DirectoryEntry entry in Searcher.SearchRoot.Children.Where(ent => ent.Path == EntryPath))
{
Console.WriteLine(entry.Path);WorkingTreeNode = new TreeNode(entry.Name); // ?????? treeView1.Nodes.Add(WorkingTreeNode); if (ShouldAddNode(entry.SchemaClassName)) { WorkingTreeNode.Nodes.Add(GetChildNode(entry)); } } }
}
Note: this code "sketch" has not been compiled/tested; use it for ideas, only.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12