Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

When you are calling DirectorySearcher.FindAll() and there is  a chance to have more than 1000 records back, you must set PageSize to non-zero value, preferably 1000. Otherwise only the first 1000 records will be returned and other entries will be missed without any warning.

The names of DirectorySearcher members  and documentation is quite messy. FindAll() method should return ALL records, not the first 1000. Setting PageSize doesn't mean that you get back only single page, but triggers returning of the values into "page size" chunks transparently to you in the background. If you set SizeLimit to a value that is larger than
the server-determined default of 1000 entries, the server-determined default is used.

Thanks to Joe Kaplan's post that finally helped to find solution.

I have a simple program that imported AD users to application database. It was tested and worked fine on a few sites.

However one of our customers complained that some users were missed during import when others were imported successfully. No pattern was found what is the difference between imported and missed user accounts.

After some investigation it was noticed that only 1000 records are returned by DirectorySearcher.FindAll.  Ok, documentation describes DirectorySearcher.SizeLimit with default value is zero, which means to use the server-determined default size limit of 1000 entries. I've changed SizeLimit to 100000, but it didn't help -still only the first 1000 was returned. 
Reading further, I found that the server-determined default is used so SizeLimit is useless, if you expect  big number of results. In a few posts I found how to change the default value on the server, but there were also recommendations not to change server setting, but use paging instead. OK but how to use paging? I've started to search for code examples, expecting that some complicated coding will be required.

Finally  Joe Kaplan's post explains that only code change that required is
PageSize=1000
and all paging will happen in background.

MS should have better  DirectorySearcher documentation and implement background paging by default.

Posted on Tuesday, December 20, 2005 7:01 AM .Net Framework , Active Directory | Back to top


Comments on this post: DirectorySearcher.FindAll() -should have PageSize=1000

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Problem solved! Thank you sooooooo much for posting this solution!
Left by JB on Feb 08, 2006 9:12 PM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Bhai u r God. You saved many lives...^:)^
Left by Tortured Soul on Jul 15, 2006 2:35 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Good solution. It helped me fixing search issues in my program.
Left by TS on Sep 07, 2006 12:42 PM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thanks for your post. It helped me fix my problems. I agree the MSDN docs should be more clear about this issue.
Left by Leon Zandman on Sep 22, 2006 3:25 PM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thank you so much for posting this tip !!
Left by FTriana on Dec 01, 2006 4:11 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thanks !!! Thanks !!! this info has helped me to solve the problem quickly....
Left by Abinayaa on Apr 03, 2007 1:57 PM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
THANKS A LOT!!!
Left by M4k on May 29, 2007 1:00 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thanks a lot !!! At last I reached to the information which I wanted. I spent 2 Hrs on google before I reach here.
Left by Pruthvi on Jun 22, 2007 3:52 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
I already did that. I have a different issue. My computer gets bogged down when the result set gets to ~ 1800. I set the page size to 1000. I checked the pagefile usage and found that it was more than double of what it usually is.

Did anyone else encounter this issue?
Left by Aneesh Aravind on Jul 27, 2007 7:27 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Try a number greater than 1000 for example 2000....
Left by Mow on Nov 12, 2007 8:58 PM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Like other responses, I searched for hours on dealing with more than 1000 objects. This did it.. thanx for sharing.
Left by DeLo on Dec 17, 2007 5:34 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
THANKS!!
Left by jl on Jan 09, 2008 12:36 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thanks,Helped me as wel.I was also perplexed by this problem.It saved me a lots of time.
Left by Dave on Feb 26, 2008 12:07 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thanks, great solution!
I tried many other ways without success.
Left by Talat on Jul 03, 2008 4:04 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
The solution is working fine for me. Thanks a lot! But it is strange that nobody is complaining about Microsoft for this stupid bug! :|
Left by Mahfuz on Aug 10, 2008 4:20 PM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thanks for this. I'll test this for my customer and see if it works.
Left by Noah on Aug 27, 2008 8:26 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Ditto for me guys!! Been wrestling with this and finally you have given the answer. Have thousands of groups, but not returning them all. Thanks Again!!
Left by Ed Sliman on Sep 09, 2008 12:16 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Really ,amazing tip
that was my problem too
thank you very much
waseem
Left by Waseem Al Mustafa on Nov 08, 2008 7:29 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
That makes like so much easier. Prior to this, I couldn't find an answer, so I just looped through the alphabet and used that for a LDAP filter [e.g.(samAccountName=a*)]
Left by GLotsapot on Mar 11, 2009 6:27 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
thank u for this,u helpe me solve the boring problem !
Left by taoboy on Apr 14, 2009 8:21 PM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thanks, saved me a lot of time. Just a pointer - you need to set them both in order for it to work:
PageSize = 1000;
SizeLimit = 100000;

And I join you on your rant about MS poor documentation. They should be ashamed at themselves. At least it brings visitors to our blogs :)
Left by KeeperOfLogic on Jun 03, 2009 12:54 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
How did you loop to get the 1000+ users
Left by Karan on Aug 11, 2009 7:54 PM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Hi Karan,
Example from MSDN "Getting Search Results"
http://msdn.microsoft.com/en-us/library/ms180881.aspx

results = mySearcher.FindAll();

// Iterate through each SearchResult in the SearchResultCollection.
foreach (SearchResult searchResult in results)
Left by Michael Freidgeim on Aug 12, 2009 8:04 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Great tip, thanks for mentioning that it was great , I loved it
Left by web development company on Aug 14, 2009 11:29 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
you saved many lives. Thank you.
Left by raju datla on Nov 04, 2009 7:07 PM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thankyou for posting this.
Left by Richard on Jan 25, 2010 5:12 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
It was helpful. Thanks so much!
Left by WCX on Jan 27, 2010 10:52 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thank you! You saved me lot a time.
Without "PageSize=1000" string my program returned only 6002 user records, after adding it - 6057.
Left by Mikhail on Dec 02, 2010 4:01 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thanks a lot. Yeah agree with you, MS doesn't have proper documentation for it.
Left by Anil on Dec 16, 2010 12:05 PM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thanks, had the same Problem!
Left by Marcus on Apr 28, 2011 7:33 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thanks for posting this. I now know why I was only getting 10,000 results from Active Directory.
Left by Constantine on May 09, 2011 9:07 PM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thank you very much. It's that simple. Just change the value to a different value.
Left by Walter on Jul 25, 2011 9:19 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thank you for the tip. Very helpful. I made some tests on my side. Seems the returned results are unlimited each time the specified SizeLimit is greater than or equal to the PageSize. If it's not then the count of the returned records is equal to the value specified in SizeLimit.
Left by Anis Achek on Sep 26, 2011 2:38 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Thanx a lot. It saved my hours and its really worth.
Left by ParesH on Mar 20, 2013 2:16 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
string adpath = "GC://DC=ad,DC=flextronics,DC=com";
System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry(adpath, Request.Cookies["userid"].Value.ToString(), Request.Cookies["password"].Value.ToString());
object obj = entry.NativeObject;
System.DirectoryServices.DirectorySearcher search = new System.DirectoryServices.DirectorySearcher(entry);
search.Filter = "(&(objectClass=group)(samaccountname=" + Label1.Text.ToString() + "))";
search.SearchScope = System.DirectoryServices.SearchScope.Subtree;
search.PropertiesToLoad.Add("member");
search.PropertiesToLoad.Add("distinguishedName");
search.PageSize = 1000;

System.DirectoryServices.SearchResultCollection src = search.FindAll();
foreach (System.DirectoryServices.SearchResult sr in src)
{
}
Left by Sivaraman G on Jan 24, 2014 12:31 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
in my group iam having 9000 members. but i cant able to retireve here using this code can any one help me to fix it.i am trying this for 2 days please help me to fix it.
Left by Sivaraman G on Jan 24, 2014 12:34 AM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
Very useful blog post. Thank you for sharing!
Left by Fanda on Aug 04, 2014 11:19 PM

# re: DirectorySearcher.FindAll() -should have PageSize=1000
Requesting Gravatar...
thanks, worked for me.
Left by J on Nov 22, 2014 3:49 AM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net