1 out of 1 rated this helpful - Rate this topic

DirectorySearcher.FindAll Method

Executes the search and returns a collection of the entries that are found.

Namespace:  System.DirectoryServices
Assembly:  System.DirectoryServices (in System.DirectoryServices.dll)
public SearchResultCollection FindAll()

Return Value

Type: System.DirectoryServices.SearchResultCollection
A 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.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
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.
Remember Pagesize
this will only return the first 1000 items unless you set the pagesize.
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);
            }