Q: searching active directory

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
Here is my code that I got from internet and I am assuming this give me
e-mail address of the people in the active directory.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim rootEntry As New DirectoryEntry("GC://dc=oecc,dc=net")
Dim searcher As New DirectorySearcher(rootEntry)
searcher.PropertiesToLoad.Add("mail")

Dim results As SearchResultCollection
results = searcher.FindAll()

Dim result As SearchResult

For Each result In results
Response.Write(result.Properties("mail")(0))
Next

End Sub

I got “Object reference not set to an instance of an object†at line
Response.Write(result.Properties("mail")(0)), what am I missing?
Thanks,
Jim.
 
I whci somebody could tell me what I am missing, I got the code from that
site but not workign for me.
 
The server which is managing the
Active Directory you want to search.

You'll also need to feed the name and password
of a user authorized to access the LDAP server.

C#
DirectoryEntry rootentry = new DirectoryEntry(LDAP://ADservername,
"username","password");

VB.NET
Dim rootEntry As New DirectoryEntry(LDAP://ADservername,
"username","password")

See:
http://www.codeproject.com/aspnet/activedirectoryuse.asp





Juan T. Llibre
ASP.NET MVP
===========
 
Your LDAP path should look like this :

LDAP://corp.bedking.com/CN=Users,DC=corp,DC=bedking,DC=com



Juan T. Llibre
ASP.NET MVP
===========
 
Thanks Juan,
It hellped well. I still have a problem though. This code gives me the
display name from active directory for myUser, I was trying to achive e-mail
of the user, how should I get it?


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim rootEntry As New DirectoryEntry("LDAP://DC=MYDOMAIN,DC=net",
"MYDOMAIN\Admin", "myPassword")

Dim searcher As New DirectorySearcher(rootEntry)
searcher.PropertiesToLoad.Add("cn")
searcher.PropertiesToLoad.Add("mail")

searcher.Filter = "(&(anr=myUser)(objectCategory=person))"

Dim results As SearchResultCollection
results = searcher.FindAll()

Dim result As SearchResult

For Each result In results
Response.Write(result.GetDirectoryEntry.Name)
Next

End Sub
Thanks,
Jim.
 
Juan,
Thanks for your help. your are very helpful. when I did,
searcher.Filter = "(&(anr=myUser)(objectCategory=mail))"
this did not give anything. I am using
Response.Write(result.GetDirectoryEntry.Name) to display result, is this
correct?
 
Juan,
I use this at the filter
searcher.Filter = "(&(anr=myUser)(objectCategory=person))"
and this to see the result,
Response.Write(result.Properties("mail")(0))
It seems I am getting active directory E-mail information for myUser.
Problem is when user does not have anything defined in the e-mail field,
response.write fails. How should I prevent from that?
Thanks,
Jim.
 
Try

Response.Write(Result.GetDirectoryEntry().Properties("mail").Value)

C'ya tomorrow! I'm outtahere.

Going to see a movie ( need to blow off some steam )




Juan T. Llibre
ASP.NET MVP
===========
 
OK...

You should check for null values,
and replace the null value with a space " ".

Sorry, I don't have time for real code right now.

pseudo code :

If IsNull(result.Properties("mail")(0)) Then
result.Properties("mail")(0) = " "
else
Response.Write(result.Properties("mail")(0))
End if

You might have to do some syntax-checking.
I'm on the run to the movies...




Juan T. Llibre
ASP.NET MVP
===========
 
Back
Top