Directory Services

Setting the Search Scope

To increase search performance, limit the search scope search to a single object or subset of objects. For this task, DirectorySearcher provides the SearchScope property.

The search scope can be set to one of the three following settings:

The following diagram illustrates how each of these scopes fits within your domain hierarchy. For more information, see Search Scope.

domain search scopes

The following code example shows how to use the SearchScope property to search a subtree.

DirectoryEntry entry = new DirectoryEntry("LDAP://CN=users,DC=fabrikam,DC=com");
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.SearchScope = SearchScope.Subtree;
mySearcher.Filter = "(&(objectClass=user)(anr=test*))";
SearchResultCollection ResEnt = mySearcher.FindAll();
	// Handle results.
// Handle exceptions.