0 out of 2 rated this helpful - Rate this topic

DirectorySearcher.FindOne Method

Executes the search and returns only the first entry that is found.

Namespace:  System.DirectoryServices
Assembly:  System.DirectoryServices (in System.DirectoryServices.dll)
public SearchResult FindOne()

Return Value

Type: System.DirectoryServices.SearchResult
A SearchResult object that contains the first entry that is found during the search.

If more than one entry is found during the search, only the first entry is returned. If no entries are found to match the search criteria, a null reference (Nothing in Visual Basic) is returned.

.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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Throws Exception section please
Can someone document what exceptions this method throws please?
FindOne() throws Exception instead of returning null
The documentation says FindOne() returns null instead of throwing exception instead of throwing exception.

However, I got the following exception from FindOne():

System.DirectoryServices.DirectoryServicesCOMException was unhandled
  Message="There is no such object on the server.\r\n"
  Source="System.DirectoryServices"
  ErrorCode=-2147016656
  ExtendedError=8333
  ExtendedErrorMessage="0000208D: NameErr: DSID-031522C9, problem 2001 (NO_OBJECT), data 0, best match of:\n\t'OU=HABObjects,DC=***,DC=***,DC=***,DC=***'\n"
  StackTrace:
       at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
       at System.DirectoryServices.DirectoryEntry.Bind()
       at System.DirectoryServices.DirectoryEntry.get_AdsObject()
       at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
       at System.DirectoryServices.DirectorySearcher.FindOne()
       at ActiveDirectoryStub.ADUtilities.QueryActiveDirectorySingleton(String query, String filter) in SomePath\ADUtilities.cs:line 79
       at ActiveDirectoryStub.ADUtilities.QueryActiveDirectoryWithCacheSingleton(String query, String filter) in SomePath\ADUtilities.cs:line 127
       at ActiveDirectoryStub.ActiveDirectoryStub.AddUsersFromGroup(String groupDistinguishedName) in SomePath\ActiveDirectoryStub.cs:line 172
       at ActiveDirectoryStub.ActiveDirectoryStub.AddUsers() in SomePath\ActiveDirectoryStub.cs:line 138
       at ActiveDirectoryStub.ActiveDirectoryStub.Add() in SomePath\ActiveDirectoryStub.cs:line 100
       at BaazStubsConsole.Program.Main(String[] args) in SomePath\Program.cs:line 98
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: