[英]Can I get more than 1000 records from a DirectorySearcher?
I just noticed that the return list for results is limited to 1000. I have more than 1000 groups in my domain (HUGE domain).我刚刚注意到结果的返回列表限制为 1000。我的域(巨大域)中有 1000 多个组。 How can I get more than 1000 records?如何获得超过 1000 条记录? Can I start at a later record?我可以从稍后的记录开始吗? Can I cut it up into multiple searches?我可以把它分成多个搜索吗?
Here is my query:这是我的查询:
DirectoryEntry dirEnt = new DirectoryEntry("LDAP://dhuba1kwtn004");
string[] loadProps = new string[] { "cn", "samaccountname", "name", "distinguishedname" };
DirectorySearcher srch = new DirectorySearcher(dirEnt, "(objectClass=Group)", loadProps);
var results = srch.FindAll();
I have tried to set srch.SizeLimit = 2000;我试图设置srch.SizeLimit = 2000; , but that doesn't seem to work. ,但这似乎不起作用。 Any ideas?有任何想法吗?
You need to set DirectorySearcher.PageSize to a non-zero value to get all results. 您需要将DirectorySearcher.PageSize设置为非零值以获取所有结果。
BTW you should also dispose DirectorySearcher when you're finished with it 顺便说一句,您还应该在处理完DirectorySearcher后再对其进行处置
using(var srch = new DirectorySearcher(dirEnt, "(objectClass=Group)", loadProps))
{
srch.PageSize = 1000;
var results = srch.FindAll();
}
The API documentation isn't very clear, but essentially: API文档不是很清楚,但是本质上是:
when you do a paged search, the SizeLimit is ignored, and all matching results are returned as you iterate through the results returned by FindAll. 当您进行分页搜索时,将忽略SizeLimit,并且在您遍历FindAll返回的结果时将返回所有匹配的结果。 Results will be retrieved from the server a page at a time. 结果将一次从服务器检索到一页。 I chose the value of 1000 above, but you can use a smaller value if preferred. 我选择了1000以上的值,但是如果愿意,可以使用较小的值。 The tradeoff is: using a small PageSize will return each page of results faster, but will require more frequent calls to the server when iterating over a large number of results. 折衷方案是:使用较小的PageSize将更快地返回结果的每一页,但在迭代大量结果时将需要更频繁地调用服务器。
by default the search isn't paged (PageSize = 0). 默认情况下,不分页搜索(PageSize = 0)。 In this case up to SizeLimit results is returned. 在这种情况下,最多返回SizeLimit结果。
As Biri pointed out, it's important to dispose the SearchResultCollection returned by FindAll, otherwise you may have a memory leak as described in the Remarks section of the MSDN documentation for DirectorySearcher.FindAll . 正如Biri所指出的,处置由FindAll返回的SearchResultCollection很重要,否则,可能会发生内存泄漏,如DirectorySearcher.FindAll的MSDN文档的备注部分所述 。
One way to help avoid this in .NET 2.0 or later is to write a wrapper method that automatically disposes the SearchResultCollection. 在.NET 2.0或更高版本中,避免这种情况的一种方法是编写一个自动处理SearchResultCollection的包装器方法。 This might look something like the following (or could be an extension method in .NET 3.5): 这可能看起来像以下内容(或可能是.NET 3.5中的扩展方法):
public IEnumerable<SearchResult> SafeFindAll(DirectorySearcher searcher)
{
using(SearchResultCollection results = searcher.FindAll())
{
foreach (SearchResult result in results)
{
yield return result;
}
} // SearchResultCollection will be disposed here
}
You could then use this as follows: 然后,您可以按以下方式使用它:
using(var srch = new DirectorySearcher(dirEnt, "(objectClass=Group)", loadProps))
{
srch.PageSize = 1000;
var results = SafeFindAll(srch);
}
I tried to use given solution to achieve pagination but it doesnt work.我尝试使用给定的解决方案来实现分页,但它不起作用。 I set pageSize = 100;我设置 pageSize = 100; and i get all the items through searchResult.我通过searchResult获得所有项目。
dirSearcher = new DirectorySearcher(direntry); dirSearcher = new DirectorySearcher(direntry); dirSearcher.Filter = ("(|(objectClass=volume). (objectClass=user)(objectClass=printQueue)(objectClass=computer). (objectClass=organizationalUnit)(objectClass=Group))"); dirSearcher.Filter = ("(|(objectClass=volume).(objectClass=user)(objectClass=printQueue)(objectClass=computer).(objectClass=organizationalUnit)(objectClass=Group))");
dirSearcher.PageSize = 100;
dirSearcher.SearchScope = System.DirectoryServices.SearchScope.Subtree;
dirSearcher.ServerTimeLimit = new TimeSpan(1000);
//dirSearcher.VirtualListView = new DirectoryVirtualListView(0, 100, 1);
using (SearchResultCollection results = dirSearcher.FindAll())
{
foreach (SearchResult result in results)
{
DirectoryEntry ent = result.GetDirectoryEntry();
ADItem ProviderItem = Context.ConvertToItem(ent, true);
if (ProviderItem != null)
{
ProviderItem.IsPartialData = true;
ProviderItems.Add(ProviderItem);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.