468,458 Members | 1,858 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

Q: searching active directory

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.

Nov 19 '05 #1
12 1420
Hi, Jim.

Have you had a look at :

http://msdn.microsoft.com/library/de...etadsearch.asp

?
Juan T. Llibre
ASP.NET MVP
===========
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
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.


Nov 19 '05 #2
I whci somebody could tell me what I am missing, I got the code from that
site but not workign for me.

"Juan T. Llibre" wrote:
Hi, Jim.

Have you had a look at :

http://msdn.microsoft.com/library/de...etadsearch.asp

?
Juan T. Llibre
ASP.NET MVP
===========
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
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.


Nov 19 '05 #3
Shouldn't your DirectoryEntry Constructor start with LDAP:// ?

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
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.

Nov 19 '05 #4
yes but it requires a server name, which server I need to give?

"David Jessee" wrote:
Shouldn't your DirectoryEntry Constructor start with LDAP:// ?

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
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.


Nov 19 '05 #5
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/ac...rectoryuse.asp

Juan T. Llibre
ASP.NET MVP
===========
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:66**********************************@microsof t.com...
yes but it requires a server name, which server I need to give?

"David Jessee" wrote:
Shouldn't your DirectoryEntry Constructor start with LDAP:// ?

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
> 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.
>
>
>


Nov 19 '05 #6
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
===========
"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...
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/ac...rectoryuse.asp

Juan T. Llibre
ASP.NET MVP
===========
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:66**********************************@microsof t.com...
yes but it requires a server name, which server I need to give?

"David Jessee" wrote:
Shouldn't your DirectoryEntry Constructor start with LDAP:// ?

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
> 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.
>
>
>


Nov 19 '05 #7
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 T. Llibre" wrote:
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/ac...rectoryuse.asp

Juan T. Llibre
ASP.NET MVP
===========
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:66**********************************@microsof t.com...
yes but it requires a server name, which server I need to give?

"David Jessee" wrote:
Shouldn't your DirectoryEntry Constructor start with LDAP:// ?

"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
> 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.
>
>
>


Nov 19 '05 #8
You'd need to modify the Filter.

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

Changing the objectCategory to the
category for "mail" should do it.

See a complete listing for LDAP objectCategories at:
http://dotnetjunkies.com/WebLog/bria.../31/40012.aspx


Juan T. Llibre
ASP.NET MVP
===========
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:D5**********************************@microsof t.com...
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 T. Llibre" wrote:
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/ac...rectoryuse.asp

Juan T. Llibre
ASP.NET MVP
===========
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:66**********************************@microsof t.com...
> yes but it requires a server name, which server I need to give?
>
> "David Jessee" wrote:
>
>> Shouldn't your DirectoryEntry Constructor start with LDAP:// ?
>>
>> "JIM.H." <JI**@discussions.microsoft.com> wrote in message
>> news:61**********************************@microsof t.com...
>> > 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.
>> >
>> >
>> >
>>
>>
>>


Nov 19 '05 #9
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 T. Llibre" wrote:
You'd need to modify the Filter.

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

Changing the objectCategory to the
category for "mail" should do it.

See a complete listing for LDAP objectCategories at:
http://dotnetjunkies.com/WebLog/bria.../31/40012.aspx


Juan T. Llibre
ASP.NET MVP
===========
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:D5**********************************@microsof t.com...
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 T. Llibre" wrote:
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/ac...rectoryuse.asp

Juan T. Llibre
ASP.NET MVP
===========
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:66**********************************@microsof t.com...
> yes but it requires a server name, which server I need to give?
>
> "David Jessee" wrote:
>
>> Shouldn't your DirectoryEntry Constructor start with LDAP:// ?
>>
>> "JIM.H." <JI**@discussions.microsoft.com> wrote in message
>> news:61**********************************@microsof t.com...
>> > 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.
>> >
>> >
>> >
>>
>>
>>


Nov 19 '05 #10
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.
"JIM.H." wrote:
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 T. Llibre" wrote:
You'd need to modify the Filter.

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

Changing the objectCategory to the
category for "mail" should do it.

See a complete listing for LDAP objectCategories at:
http://dotnetjunkies.com/WebLog/bria.../31/40012.aspx


Juan T. Llibre
ASP.NET MVP
===========
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:D5**********************************@microsof t.com...
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 T. Llibre" wrote:

> 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/ac...rectoryuse.asp
>
>
>
>
>
> Juan T. Llibre
> ASP.NET MVP
> ===========
> "JIM.H." <JI**@discussions.microsoft.com> wrote in message
> news:66**********************************@microsof t.com...
> > yes but it requires a server name, which server I need to give?
> >
> > "David Jessee" wrote:
> >
> >> Shouldn't your DirectoryEntry Constructor start with LDAP:// ?
> >>
> >> "JIM.H." <JI**@discussions.microsoft.com> wrote in message
> >> news:61**********************************@microsof t.com...
> >> > 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.
> >> >
> >> >
> >> >
> >>
> >>
> >>
>
>
>


Nov 19 '05 #11
Try

Response.Write(Result.GetDirectoryEntry().Properti es("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
===========
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:76**********************************@microsof t.com...
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 T. Llibre" wrote:
You'd need to modify the Filter.

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

Changing the objectCategory to the
category for "mail" should do it.

See a complete listing for LDAP objectCategories at:
http://dotnetjunkies.com/WebLog/bria.../31/40012.aspx


Juan T. Llibre
ASP.NET MVP
===========
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:D5**********************************@microsof t.com...
> 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 T. Llibre" wrote:
>
>> 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/ac...rectoryuse.asp
>>
>>
>>
>>
>>
>> Juan T. Llibre
>> ASP.NET MVP
>> ===========
>> "JIM.H." <JI**@discussions.microsoft.com> wrote in message
>> news:66**********************************@microsof t.com...
>> > yes but it requires a server name, which server I need to give?
>> >
>> > "David Jessee" wrote:
>> >
>> >> Shouldn't your DirectoryEntry Constructor start with LDAP:// ?
>> >>
>> >> "JIM.H." <JI**@discussions.microsoft.com> wrote in message
>> >> news:61**********************************@microsof t.com...
>> >> > 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.
>> >> >
>> >> >
>> >> >
>> >>
>> >>
>> >>
>>
>>
>>


Nov 19 '05 #12
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
===========
"JIM.H." <JI**@discussions.microsoft.com> wrote in message
news:2C**********************************@microsof t.com...
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.
"JIM.H." wrote:
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 T. Llibre" wrote:
> You'd need to modify the Filter.
>
> searcher.Filter = "(&(anr=myUser)(objectCategory=person))"
>
> Changing the objectCategory to the
> category for "mail" should do it.
>
> See a complete listing for LDAP objectCategories at:
> http://dotnetjunkies.com/WebLog/bria.../31/40012.aspx
>
>
>
>
> Juan T. Llibre
> ASP.NET MVP
> ===========
> "JIM.H." <JI**@discussions.microsoft.com> wrote in message
> news:D5**********************************@microsof t.com...
> > 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 T. Llibre" wrote:
> >
> >> 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/ac...rectoryuse.asp
> >>
> >>
> >>
> >>
> >>
> >> Juan T. Llibre
> >> ASP.NET MVP
> >> ===========
> >> "JIM.H." <JI**@discussions.microsoft.com> wrote in message
> >> news:66**********************************@microsof t.com...
> >> > yes but it requires a server name, which server I need to give?
> >> >
> >> > "David Jessee" wrote:
> >> >
> >> >> Shouldn't your DirectoryEntry Constructor start with LDAP:// ?
> >> >>
> >> >> "JIM.H." <JI**@discussions.microsoft.com> wrote in message
> >> >> news:61**********************************@microsof t.com...
> >> >> > 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.
> >> >> >
> >> >> >
> >> >> >
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>
>
>
>

Nov 19 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by scoomey | last post: by
1 post views Thread by tangus via DotNetMonster.com | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.