DirectoryService Query... Help

G

Guest

Hi can someone please tell me how I change this directory service query so
that it searches through each record in the active directory and returns all
the accounts!

At the moment I can only get the query to return one account at a time!

I presume I have to create some kind of loop on the following so I return
all the users SAM info????

Dim SamAccount As String = Right(inSAM, Len(inSAM) - InStr(inSAM, "\"))

I would be really grateful for any help!

Thanks


<code>

Public Function GetUserInfo(ByVal inSAM As String, ByVal inType As String)
As String
Try

Dim sPath As String = "LDAP://netdomain.domain.com"
Dim SamAccount As String = Right(inSAM, Len(inSAM) -
InStr(inSAM, "\"))
'pass the user account and password for your Admin Account.
Dim myDirectory As New DirectoryEntry(sPath, "administrator",
"password")
Dim mySearcher As New DirectorySearcher(myDirectory)
Dim mySearchResultColl As SearchResultCollection
Dim mySearchResult As SearchResult
Dim myResultPropColl As ResultPropertyCollection
Dim myResultPropValueColl As ResultPropertyValueCollection
'Build LDAP query
mySearcher.Filter = ("(&(objectClass=user)(samaccountname=" &
SamAccount & "))")
mySearchResultColl = mySearcher.FindAll()

</code>
 
S

Scott Allen

Hi Tim:

The filter seems to be looking for a specific user account by
including samAccountName. Have you tried removing it from the filter?
 
G

Guest

Hi Scott,

Thanks for the advice! The trouble is that it returns the same user
information again and again! Do I have to put a

For Each mySearchResultColl In mySearcher.FindAll()

and if so where???

I really appritiate the advice!
Thanks


<code>

Public Function GetUserInfo(ByVal inType As String) As String
Try

Dim sPath As String = "LDAP://netdomain.domain.com"
'Dim SamAccount As String = Right(inSAM, Len(inSAM) -
InStr(inSAM, "\"))
'pass the user account and password for your Admin Account.
Dim myDirectory As New DirectoryEntry(sPath, "administrator",
"password")
Dim mySearcher As New DirectorySearcher(myDirectory)
Dim mySearchResultColl As SearchResultCollection
Dim mySearchResult As SearchResult
Dim myResultPropColl As ResultPropertyCollection
Dim myResultPropValueColl As ResultPropertyValueCollection
'Build LDAP query
mySearcher.Filter = ("(&(objectClass=user))")
mySearchResultColl = mySearcher.FindAll()
'I expect only one user from search result
Select Case mySearchResultColl.Count
Case 0
Return "Null"
Exit Function

End Select
'Get the search result from the collection
mySearchResult = mySearchResultColl.Item(0)

'Get the Properites, they contain the usefull info
myResultPropColl = mySearchResult.Properties

'displayname, mail
'Retrieve from the properties collection the display name and
email of the user
myResultPropValueColl = myResultPropColl.Item(inType)
Return CStr(myResultPropValueColl.Item(0))

Catch ex As System.Exception

'do some error return here.
End Try
End Function
 
S

Scott Allen

Hi Tim:

Yes, you want to loop through mySearchResultColl with For Each or
other looping logic. Currently the code only appears to be looking at
Item(0) - the first object in the collection.
 
G

Guest

Thanks for the HELP!

I don't suppose you know any really good resources for directory services
and ADSI??? I'm trying to create a web app that allows one of our users to
update certain aspects of the AD!

Also want to create a contacts app that pulls details from the AD!

Thanks AGAIN...
 
S

Scott Allen

15seconds.com used to have a lot of good ADSI info, but it doesn't
look like they have updated the site in quite some time. It is one of
those sticky tar pit like areas that people try to avoid,
unfortunately for you :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top