سؤال

I am binding active directory entries into my asp.net treeview control but not able to achieve it completely. I want to bind the AD entries into my treeview as it looks like same as in the Active Directory tree hierarchy.

I can able to bind the Main OUs but I am not able to bind its child of child and also some of the Ou's children may have children and some of them or not having any children.

I am not sure about the last end so I am not sure about the for loop count, it may goes like below,

A

|__A1

   |__A11

      |_A111

        |_...
B

|__B1

|__B2

C

 |__C1

    |__C11

So In this case, How can i construct my treeview to get these structure of active directory? I dont know how to proceed this with for loop. Please help me to achieve this structure on my treeview1.

Here is My code:

DirectoryEntry ADentry = new DirectoryEntry("LDAP://10.36.6.163/DC=server,DC=local", AD.LDAPUser, AD.Password, AuthenticationTypes.Secure);

    DirectorySearcher Searcher = new DirectorySearcher(ADentry);
    Searcher.Filter = ("(objectClass=organizationalUnit)");
    foreach (DirectoryEntry firstChild in ADentry.Children)
    {
        if (firstChild.Name.Contains("OU"))
        {
            TreeNode Node = new TreeNode(firstChild.Name.Remove(0,3));
            TreeView1.Nodes.Add(Node);

            foreach (DirectoryEntry secondchild in firstChild.Children)
            {

                TreeNode childNode=new TreeNode(secondchild.Name.Remove(0,3));
                Node.ChildNodes.Add(childNode);

                if (secondchild.Children != null)
                {
                    ??????
                }
            }
        }
    }

By using this code I can able to bind main OUs of root node only as mentioned below, not able to get its child of child. Need to bind the nodes the corresponding child with corresponding parents.

A
B
C
هل كانت مفيدة؟

المحلول

First, you should search for all entries (instead of only OUs) and use Searcher.FindOne().GetDirectoryEntry().Children to get the results instead of ADentry.Children.

When specifying all entries ((objectClass=*)), the first returned result is always the root domain.

Second, you should filter the result's SchemaClassName to remove entries that you are not interested in.

So for example,

DirectoryEntry ADentry = new DirectoryEntry("LDAP://10.36.6.163/DC=server,DC=local", AD.LDAPUser, AD.Password, AuthenticationTypes.Secure);

DirectorySearcher Searcher = new DirectorySearcher(ADentry);
Searcher.Filter = ("(objectClass=*)");  // Search all.

// The first item in the results is always the domain. Therefore, we just get that and retrieve its children.
foreach (DirectoryEntry entry in Searcher.FindOne().GetDirectoryEntry().Children)
{
    if (ShouldAddNode(entry.SchemaClassName))
        TreeView1.Nodes.Add(GetChildNode(entry));
}

The GetChildNode() method is defined as follows:

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;
}

Note: The ShouldAddNode() method is used to filter only useful node types, such as "organizationalUnit". Other possibly useful node types for you are "group", "computer", "user", "contact".

نصائح أخرى

Old topic...

If you're searching for everything simply to find the root then why search at all? You have the root at the first line... Code below will give you exactly the same result.

DirectoryEntry ADentry = new DirectoryEntry("LDAP://10.36.6.163/DC=server,DC=local", AD.LDAPUser, AD.Password, AuthenticationTypes.Secure);
foreach (DirectoryEntry entry in ADentry.Children)
{
    if (ShouldAddNode(entry.SchemaClassName))
        TreeView1.Nodes.Add(GetChildNode(entry));
}

If you want the root in the treeview it's even easier.

DirectoryEntry ADentry = new DirectoryEntry("LDAP://10.36.6.163/DC=server,DC=local", AD.LDAPUser, AD.Password, AuthenticationTypes.Secure);
TreeView1.Nodes.Add(GetChildNode(ADentry));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top