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:

  • Base. Searches the bound object and return only that object if it matches the search criterion. For example, if you are bound to the domain, then it will search everything in that domain and return only the bound object.
  • OneLevel. Searches all objects that are contained in the same level as the bound object. For example, if you are bound to a group, it will search all groups or other objects that are at the same level as that group.
  • Subtree. Searches all objects that are contained in the sub-tree of the bound object, including the base object. For example, if you are bound to a server, it will search all objects in the hierarchy that are on the same level or under that server. This is the default value. If you perform a directory synchronization search with a DirectorySynchronization object, the application is required to specify Subtree scope.

The following illustration shows how each of these scopes fits within your domain hierarchy.

Domain search scopes

The following C# code example shows how to use the SearchScope property to search a sub-tree.

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.

See Also

Reference

System.DirectoryServices
DirectorySearcher
SearchScope
DirectorySynchronization

Concepts

Searching the Directory

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.