1 out of 7 rated this helpful - Rate this topic

DirectorySearcher Class

Performs queries against Active Directory Domain Services.

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.DirectoryServices.DirectorySearcher

Namespace:  System.DirectoryServices
Assembly:  System.DirectoryServices (in System.DirectoryServices.dll)
[DSDescriptionAttribute("DirectorySearcherDesc")]
[DirectoryServicesPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
public class DirectorySearcher : Component

The DirectorySearcher type exposes the following members.

  Name Description
Public method DirectorySearcher() Initializes a new instance of the DirectorySearcher class with default values.
Public method DirectorySearcher(DirectoryEntry) Initializes a new instance of the DirectorySearcher class using the specified search root.
Public method DirectorySearcher(String) Initializes a new instance of the DirectorySearcher class with the specified search filter.
Public method DirectorySearcher(DirectoryEntry, String) Initializes a new instance of the DirectorySearcher class with the specified search root and search filter.
Public method DirectorySearcher(String, String[]) Initializes a new instance of the DirectorySearcher class with the specified search filter and properties to retrieve.
Public method DirectorySearcher(DirectoryEntry, String, String[]) Initializes a new instance of the DirectorySearcher class with the specified search root, search filter, and properties to retrieve..
Public method DirectorySearcher(String, String[], SearchScope) Initializes a new instance of the DirectorySearcher class with the specified search filter, properties to retrieve, and search scope.
Public method DirectorySearcher(DirectoryEntry, String, String[], SearchScope) Initializes a new instance of the DirectorySearcher class with the specified search root, search filter, properties to retrieve, and search scope.
Top
  Name Description
Public property Asynchronous Gets or sets a value that indicates if the search is performed asynchronously.
Public property AttributeScopeQuery Gets or sets the LDAP display name of the distinguished name attribute to search in. Only one attribute can be used for this type of search.
Public property CacheResults Gets or sets a value indicating whether the result is cached on the client computer.
Protected property CanRaiseEvents Gets a value indicating whether the component can raise an event. (Inherited from Component.)
Public property ClientTimeout Gets or sets the maximum amount of time that the client waits for the server to return results. If the server does not respond within this time, the search is aborted and no results are returned.
Public property Container Gets the IContainer that contains the Component. (Inherited from Component.)
Public property DerefAlias Gets or sets a value indicating how the aliases of objects that are found during a search should be resolved.
Protected property DesignMode Gets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)
Public property DirectorySynchronization Gets or sets an object that represents the directory synchronization control to use with the search.
Protected property Events Gets the list of event handlers that are attached to this Component. (Inherited from Component.)
Public property ExtendedDN Gets or sets a value that indicates the format of the distinguished names.
Public property Filter Gets or sets a value indicating the Lightweight Directory Access Protocol (LDAP) format filter string.
Public property PageSize Gets or sets a value indicating the page size in a paged search.
Public property PropertiesToLoad Gets a value indicating the list of properties to retrieve during the search.
Public property PropertyNamesOnly Gets or sets a value indicating whether the search retrieves only the names of attributes to which values have been assigned.
Public property ReferralChasing Gets or sets a value indicating how referrals are chased.
Public property SearchRoot Gets or sets a value indicating the node in the Active Directory Domain Services hierarchy where the search starts.
Public property SearchScope Gets or sets a value indicating the scope of the search that is observed by the server.
Public property SecurityMasks Gets or sets a value indicating which security access information for the specified attributes should be returned by the search.
Public property ServerPageTimeLimit Gets or sets a value indicating the maximum amount of time the server should search for an individual page of results. This is not the same as the time limit for the entire search.
Public property ServerTimeLimit The ServerTimeLimit property gets or sets a value indicating the maximum amount of time the server spends searching. If the time limit is reached, only entries that are found up to that point are returned.
Public property Site Gets or sets the ISite of the Component. (Inherited from Component.)
Public property SizeLimit Gets or sets a value indicating the maximum number of objects that the server returns in a search.
Public property Sort Gets or sets a value indicating the property on which the results are sorted.
Public property Tombstone Gets or sets a value indicating whether the search should also return deleted objects that match the search filter.
Public property VirtualListView Gets or sets a value indicating the virtual list view options for the search.
Top
  Name Description
Public method CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public method Dispose() Releases all resources used by the Component. (Inherited from Component.)
Protected method Dispose(Boolean) Releases the managed resources that are used by the DirectorySearcher object and, optionally, releases unmanaged resources. (Overrides Component.Dispose(Boolean).)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)
Public method FindAll Executes the search and returns a collection of the entries that are found.
Public method FindOne Executes the search and returns only the first entry that is found.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected method GetService Returns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected method MemberwiseClone() Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method MemberwiseClone(Boolean) Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public method ToString Returns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from Component.)
Top
  Name Description
Public event Disposed Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)
Top

Use a DirectorySearcher object to search and perform queries against an Active Directory Domain Services hierarchy using Lightweight Directory Access Protocol (LDAP). LDAP is the only system-supplied Active Directory Service Interfaces (ADSI) provider that supports directory searching. An administrator can make, alter, and delete objects that are found in the hierarchy. For more information, see Using System.DirectoryServices.

When you create an instance of DirectorySearcher, you specify the root you want to retrieve, and an optional list of properties to retrieve. The SearchRoot property enables you to set additional properties to do the following tasks:

  • Cache the search results on the local computer. Set the CacheResults property to true to store directory information on the local computer. Updates are made to this local cache and committed to Active Directory Domain Services only when the DirectoryEntry.CommitChanges method is called.

  • Specify the length of time to search, using the ServerTimeLimit property.

  • Retrieve attribute names only. Set the PropertyNamesOnly property to true to retrieve only the names of attributes to which values have been assigned.

  • Perform a paged search. Set the PageSize property to specify the maximum number of objects that are returned in a paged search. If you do not want to perform a paged search, set the PageSize property to its default of zero.

  • Specify the maximum number of entries to return, using the SizeLimit property. If you set the SizeLimit property to its default of zero, the server-determined default is 1000 entries.

Note Note

If the maximum number of returned entries and time limits exceed the limitations that are set on the server, the server settings override the component settings.

For a list of initial property values for an instance of the DirectorySearcher class, see the DirectorySearcher() constructor.

Note Note

It is assumed that you have a general understanding of Active Directory Domain Services before using this class. For more information, see the System.DirectoryServices overview.

.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.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Addition to PageSize
When you are calling DirectorySearcher.FindAll() and there is  a chance to have more than 1000 records back, you must set PageSize to non-zero value, preferably 1000. Otherwise only the first 1000 records will be returned and other entries will be missed without any warning. To avoid this, set PageSize to 1000. $0$0 $0 $0More - here http://geekswithblogs.net/mnf/archive/2005/12/20/63581.aspx$0