DirectorySearcher.FindAll Method
.NET Framework 4
Executes the search and returns a collection of the entries that are found.
Assembly: System.DirectoryServices (in System.DirectoryServices.dll)
Return Value
Type: System.DirectoryServices.SearchResultCollectionA SearchResultCollection object that contains the results of the search.
| Exception | Condition |
|---|---|
| InvalidOperationException |
The specified DirectoryEntry is not a container. |
| NotSupportedException |
Searching is not supported by the provider that is being used. |
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.
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.
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.
- 10/24/2011
- Strikah
Remember Pagesize
this will only return the first 1000 items unless you set the pagesize.
- 7/19/2011
- Pilot Mike
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);
}