以层次结构顺序获取活动目录中的所有内容 [英] Get all ous in active directory in hierarchical order

查看:75
本文介绍了以层次结构顺序获取活动目录中的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按层次顺序获取域中的所有OU并将其绑定到MVC中的树视图。



我尝试过:



我尝试了下面的代码,但它返回空。

I would like to get all OUs in a domain in hierarchical order and bind it to tree view in MVC.

What I have tried:

I tried below code but it returns empty.

Domain domain = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, "domainname", "username", "password"));
Forest forest = domain.Forest;
DomainCollection domains = forest.Domains;



但是如果我使用这段代码,我会得到OU列表但不是我想要的。


But if I use this code I get list of OUs but not how I want.

List<string> lstName = new List<string>();
            DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://DC=domainname,DC=com", "username", "password");
            DirectorySearcher oDirectorySearcher = new DirectorySearcher(directoryEntry,"(objectCategory=organizationalUnit)", null);

            SearchResultCollection oSearchResultCollection = oDirectorySearcher.FindAll();
            foreach (SearchResult item in oSearchResultCollection)
            {
                string name = item.Properties["name"][0].ToString();
                lstName.Add(name);
            }





有人可以帮忙吗?



谢谢



Can anyone help?

Thanks

推荐答案

AD中没有搜索功能,或者几乎没有任何系统可以返回层次结构。你必须自己构建它。



在TreeView的根目录中获取你想要的顶级项目。将这些项目添加到每个项目并为其添加一个虚拟项目,以便在它们旁边显示一个小+符号。当用户单击+展开该节点时,您将处理BeforeExpand事件。在那里,您删除虚拟节点,执行另一个查询以从该节点获取所需的子项,并将它们添加到正在扩展的节点。再次,用一个虚拟节点填充子节点以获得+到它们。



这就是它的全部。
There is no search function in AD, or pretty much any system for that matter, that will return a hierarchy. You have to build it yourself.

Get the top level items you want in the root of your TreeView. Add those items to and add a dummy item to each of them so you get the little "+" sign next to them. When the use clicks the "+" to expand that node, you handle the BeforeExpand event. In there, you remove the dummy node, do another query to get the child items you want from that node and add them to the node being expanded. Again, populate thee child nodes with a single dummy node to get the "+" to to them.

That's all there is to it.


这篇关于以层次结构顺序获取活动目录中的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆