Directory Services

Searching the Directory

The most common directory task is searching. While the DirectoryEntry object can be used to return a single directory object required for a simple operation such as binding to the domain, it cannot conduct complex searches or return large sets of results. To add a robust search operation to your application, use the DirectorySearcher object. The DirectorySearcher provides a set of properties and methods that enable you to select multiple search criteria, such as a search scope, filters, wildcard strings, page sizes for result sets, sorting, and so on.

This section discusses the following topics:

Before you perform a search, you must be familiar with the DirectorySearcher object and directory search concepts. If you previously used ADSI, then you may have used the IDirectorySearch COM interface. The DirectorySearcher object is a wrapper for IDirectorySearch.

To perform a search, your application must first bind to a root point in the directory. For optimal performance, select a point that will provide the smallest result set. Configure DirectorySearcher to provide the following parameters for your search operation:

The following diagram illustrates a search that binds to the Sales container. Decisions that must be made when setting up this search include deciding which type of object to search for and setting up a search filter for that type of object. DirectorySearcher provides a Filter property which requires a string that uses LDAP syntax as the filter string. Next, you must decide whether the scope of the filter should be at the base of the node you are bound to, through one level or through the subtree. The SearchScope property is used to set this parameter. Finally, you must decide which property names and values to return with the object.

DirectorySearcher returns a single result and its properties in a SearchResult object. Result sets are returned as a SearchResultCollection which is a container for all SearchResult objects returned by the search.

DirectorySearcher provides the following two methods to perform your search:

For more information and a procedure for creating a basic search application, see Searching Active Directory Hierarchies.