.NET Framework Class Library
DirectorySearcher.FindAll Method
Executes the search and returns a collection of the entries that are found.
Assembly: System.DirectoryServices (in System.DirectoryServices.dll)
Syntax
Visual Basic
Public Function FindAll As SearchResultCollection
C#
public SearchResultCollection FindAll()
Visual C++
public:
SearchResultCollection^ FindAll()
F#
member FindAll : unit -> SearchResultCollection
Return Value
Type: System.DirectoryServices.SearchResultCollectionA SearchResultCollection object that contains the results of the search.
Exceptions
| Exception | Condition |
|---|---|
| InvalidOperationException |
The specified DirectoryEntry is not a container. |
| NotSupportedException |
Searching is not supported by the provider that is being used. |
Remarks
Due to implementation restrictions, the SearchResultCollection class cannot release all of its unmanaged resources when it is garbage collected. To prevent a memory leak, you must call the Dispose method when the SearchResultCollection object is no longer needed.
Version Information
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supported in: 4, 3.5 SP1Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
See Also
Reference
Community Content
Strikah
Return value
In PowerShell, this method returns a SearchResult object if only one object is found, instead of a SearchResultCollection. If you want to check the number of returned objects via the Count property of SearchResultCollection, you need to code around this if the search might return only one object.
Pilot Mike
Remember Pagesize
this will only return the first 1000 items unless you set the pagesize.
scienco
Example of use
// Connect to LDAP
AuthenticationTypes authTypes; // Authentication flags.
authTypes = AuthenticationTypes.Signing | AuthenticationTypes.Sealing | AuthenticationTypes.Secure;
string domainAndUsername = domain + @"\" + user;
DirectoryEntry oAuthedEntry = new DirectoryEntry(ldapConnectionString, domainAndUsername, pwd, authTypes);
// Create new DirectorySearcher instance
DirectorySearcher search = new DirectorySearcher(oAuthedEntry);
// Set search criteria
search.Filter = "(objectCategory=user)";
search.PropertiesToLoad.Add("displayName"); // name
search.PropertiesToLoad.Add("physicalDeliveryOfficeName"); // location
search.PropertiesToLoad.Add("telephoneNumber"); // phone number
search.PropertiesToLoad.Add("mobile"); // mobile
search.PropertiesToLoad.Add("mail"); // email address
search.PropertiesToLoad.Add("msDS-UserAccountDisabled"); // account status
search.PropertiesToLoad.Add("title"); // title
search.PropertiesToLoad.Add("ms-DS-UserAccountAutoLocked"); // locked
// Perform the search
SearchResultCollection results = null;
try
{
results = search.FindAll();
}
catch (Exception searchEx)
{
throw new Exception("Error obtaining users results. ", searchEx);
}
AuthenticationTypes authTypes; // Authentication flags.
authTypes = AuthenticationTypes.Signing | AuthenticationTypes.Sealing | AuthenticationTypes.Secure;
string domainAndUsername = domain + @"\" + user;
DirectoryEntry oAuthedEntry = new DirectoryEntry(ldapConnectionString, domainAndUsername, pwd, authTypes);
// Create new DirectorySearcher instance
DirectorySearcher search = new DirectorySearcher(oAuthedEntry);
// Set search criteria
search.Filter = "(objectCategory=user)";
search.PropertiesToLoad.Add("displayName"); // name
search.PropertiesToLoad.Add("physicalDeliveryOfficeName"); // location
search.PropertiesToLoad.Add("telephoneNumber"); // phone number
search.PropertiesToLoad.Add("mobile"); // mobile
search.PropertiesToLoad.Add("mail"); // email address
search.PropertiesToLoad.Add("msDS-UserAccountDisabled"); // account status
search.PropertiesToLoad.Add("title"); // title
search.PropertiesToLoad.Add("ms-DS-UserAccountAutoLocked"); // locked
// Perform the search
SearchResultCollection results = null;
try
{
results = search.FindAll();
}
catch (Exception searchEx)
{
throw new Exception("Error obtaining users results. ", searchEx);
}
