3 out of 17 rated this helpful - Rate this topic

DirectorySearcher Class

Performs queries against Active Directory.

Namespace: System.DirectoryServices
Assembly: System.DirectoryServices (in system.directoryservices.dll)

public class DirectorySearcher : Component
public class DirectorySearcher extends Component
public class DirectorySearcher extends Component
Not applicable.

Use a DirectorySearcher object to search and perform queries against an Active Directory 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 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.

NoteNote:

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.

NoteNote:

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

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
      System.DirectoryServices.DirectorySearcher
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
DirX LDAP v3/X.500 Anonymous Connection

When using the DirectorySearcher to access a DirX system via anonymous authentication and passing a DirectoryEntry into the DirectorySearcher constructor, the username and password must be explicitly set to String.Empty and the Authentication type must be explicity set to Anonymous.

Example:

DirectoryEntry mBaseSearchPathEntry = new DirectoryEntry("LDAP://" + mLdapServer + mLdapSearchPath, String.Empty, String.Empty, AuthenticationTypes.Anonymous);

DirectorySearcher employeeFinder = new DirectorySearcher(mBaseSearchPathEntry);
Page Size / Only Returning 1000 Results

There is a delicate balance between PageSize and SizeLimit.

That is, if you don't set PageSize then you will only get 1000 results, not matter how many results there are.

Counterintuitively, increasing the SizeLimit doesn't help - instead, you should set the PageSize to be 1000. This is the only change you need to make.

The reason for this, is that SizeLimit limits the amount of results that you can retrieve at once - so your PageSize needs to be less than or equal to this. The paging is done automagically behind the scenes when you call FindAll() etc.