Question Retrieving Data from Active Directory

maydhyam

New member
Joined
Mar 1, 2012
Messages
1
Programming Experience
1-3
Hi All,

I am trying to retrieve the telephone number from Active Directory for my script, I found alot of examples online, and I tried to implement what I think should make sense (but since I really don't know vb.net that well, this is problematic). Below is the piece of code:



  1. .
  2. .
  3. .
  4. ''''Get Phone Number from Active Directory using employee ID retrieved from the logged in user

  5. Dim dirEntry, de As DirectoryEntry
  6. Dim dirSearcher As DirectorySearcher
  7. Dim sr As SearchResult

  8. dirEntry = New System.DirectoryServices.DirectoryEntry("LDAP://company.com")
  9. dirSearcher = New System.DirectoryServices.DirectorySearcher(dirEntry)
  10. dirSearcher.Filter = "(employeeID = " & Me.fp.BadgeNo & ")"

  11. 'The PropertiesToLoad.Add method will be useful when retrieving only the selected properties.
  12. dirSearcher.PropertiesToLoad.Add("telephoneNumber")
  13. 'Users Phone

  14. sr = dirSearcher.FindOne()

  15. If sr Is Nothing Then 'return false if user isn't found
  16. Me.tb_phone.Text = "N/A"
  17. End If

  18. 'Retrieve the user's Phone Number and assigns them to text boxes
  19. de = sr.GetDirectoryEntry()

  20. If Not de.Properties("telephoneNumber").Value Is Nothing Then
  21. Me.tb_phone.Text = de.Properties("telephoneNumber").Value.ToString()
  22. End If
  23. .
  24. .
  25. .


After running the script, I am getting the following error: "An operations error occurred" and the error always points to: "sr = dirSearcher.FindOne()"

In need os any assistance and or guidance...Please help

maydhyam
 
Hello,
I don't know if you will ever read this, but for other readers, i guess that the problem starts in your Entry object, you should explicitly say where to start to look for. Something like this:
VB.NET:
"LDAP://[b]CN=Users[/b],DC=domain,DC=com", dbnull, dbnull, AuthenticationTypes.Secure )
I don't know what OS you have but in 2k3 and 2k8 i couldn't find the "employeeID" attribute.
Also, the first condition no matter what sr is will go on and may break in the next line.
 

Latest posts

Back
Top