title: Get all users in the Active Directory
body: <p>I am trying to get a list of all users in the active directory on a domain. The following code is being used but doesn't seem to work:</p>
  
  <pre><code>Public Function GetAllUsers(ByVal ldapServerName As String) As Hashtable
      'To retrieve list of all  LDAP users
  
      'This function returns HashTable
      _ldapServerName = ldapServerName
  
      Dim sServerName As String = "mail"
  
      Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" &amp; ldapServerName &amp; _
            "/ou=People,dc=mydomainname,dc=com")
  
      Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)
      Dim oResults As SearchResultCollection
      Dim oResult As SearchResult
      Dim RetArray As New Hashtable()
  
      Try
  
          oSearcher.PropertiesToLoad.Add("uid")
          oSearcher.PropertiesToLoad.Add("givenname")
          oSearcher.PropertiesToLoad.Add("cn")
          oResults = oSearcher.FindAll
  
          For Each oResult In oResults
  
              If Not oResult.GetDirectoryEntry().Properties("cn").Value = "" Then
                  RetArray.Add(oResult.GetDirectoryEntry().Properties("uid").Value, _
                    oResult.GetDirectoryEntry().Properties("cn").Value)
              End If
  
          Next
  
      Catch e As Exception
  
          'MsgBox("Error is " &amp; e.Message)
          Return RetArray
  
      End Try
  
      Return RetArray
  
  End Function
  </code></pre>
  
  <p>Just to make sure I am doing this correctly, the <code>ldapServerName</code> should be the domain name that I log into that I see when I <kbd>CTRL</kbd>+<kbd>alt</kbd>+<kbd>del</kbd>, correct? Or would that go into the <code>dc=mydomainname</code> part?</p>
  
  <p>The first error in that code above is on <code>_ldapServerName = ldapServerName</code></p>
  
  <p>The error is says is:</p>
  
  <pre><code> Error 14 '_ldapServerName' is not declared. It may be inaccessible due to its protection level.
  </code></pre>
  
  <p><strong>marc_s update</strong></p>
  
  <pre><code>    ' create a domain context for your default domain
      Dim ctx As New PrincipalContext(ContextType.Domain)
  
      ' define a "query-by-example" to search for
      Dim searchExample As Principal = New UserPrincipal(ctx)
  
      ' define the principal searcher, based on that example principal
      Dim ps As New PrincipalSearcher(searchExample)
  
      ' loop over all principals found by the searcher
      For Each p As Principal In ps.FindAll()
          ' do whatever you want to do with the principals
          Console.WriteLine("Type: {0} / Name: {1}", p.StructuralObjectClass, p.Name)
      Next
  </code></pre>
  
  <p><strong>update 2</strong></p>
  
  <p>When I use IE and input <code>ldap://mydomainhere.com/ou=Users</code></p>
  
  <p>I do not get anything... But when I just do this:</p>
  
  <pre><code> ldap://mydomainhere.com
  </code></pre>
  
  <p>Then I get the "find people" box pop up. So I know I have the correct <code>LDAP</code> but not sure why the other information is preventing it from working...</p>