与PrincipalSearcher的分页

时间:2015-08-26 15:24:54

标签: c# pagination active-directory

当我使用PrincipalSearcher时,我需要分页。我尝试使用基础VirtualListView的{​​{1}}属性但没有用。

Samplecode:

DirectorySearcher

当我直接使用using System; using System.DirectoryServices; using System.DirectoryServices.AccountManagement; using System.Linq; namespace Testbed { internal class Testbed { private static void Main(string[] args) { Works(); Fails(); Console.ReadKey(); } private static void Works() { var entry = new DirectoryEntry("LDAP://server/DC=example,DC=com", @"USERNAME", "PASSWORD"); var searcher = new DirectorySearcher(entry); searcher.SearchScope = SearchScope.Subtree; searcher.Filter = "(cn=*)"; // sort is needed, otherwise DVLV will not work? searcher.Sort = new SortOption("cn", SortDirection.Ascending); searcher.VirtualListView = new DirectoryVirtualListView(5, 10, 5); SearchResultCollection result = searcher.FindAll(); Console.WriteLine("Works: " + result.Count); } private static void Fails() { var context = new PrincipalContext(ContextType.Domain, "server", "DC=example,DC=com", ContextOptions.Negotiate, @"USERNAME", "PASSWORD"); var user = new UserPrincipal(context) { DisplayName = null, }; var searcher = new PrincipalSearcher(user); ((DirectorySearcher) searcher.GetUnderlyingSearcher()).Sort = new SortOption("cn", SortDirection.Ascending); ((DirectorySearcher) searcher.GetUnderlyingSearcher()).VirtualListView = new DirectoryVirtualListView(0, 2, 2); PrincipalSearchResult<Principal> result = searcher.FindAll(); Console.WriteLine("Fails: " + result.Count()); } } } (方法:DirectorySearcher)时,一切正常,但Works()(方法:PrincipalSearcher)我得到了异常(对不起德语文本):

Fails()

任何解决方案? PrincipalSearcher甚至可以实现这一点吗?

1 个答案:

答案 0 :(得分:3)

PrincipalSearcher的默认页面大小设置为265.尝试将其设置为0,如下所示:

((DirectorySearcher) searcher.GetUnderlyingSearcher()).PageSize = 0;

这应该能够实现分页。 DirectorySearcher的默认PageSize为0,这就是它直接使用时的工作原理。