By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

DirectoryService Query... Help

P: n/a
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>
Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Tim:

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

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 28 Mar 2005 23:59:03 -0800, "Tim::.." <myatix_at_hotmail.com>
wrote:
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>


Nov 19 '05 #2

P: n/a
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

"Scott Allen" wrote:
Hi Tim:

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

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 28 Mar 2005 23:59:03 -0800, "Tim::.." <myatix_at_hotmail.com>
wrote:
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>


Nov 19 '05 #3

P: n/a
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 29 Mar 2005 07:11:02 -0800, "Tim::.." <myatix_at_hotmail.com>
wrote:
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

"Scott Allen" wrote:
Hi Tim:

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

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 28 Mar 2005 23:59:03 -0800, "Tim::.." <myatix_at_hotmail.com>
wrote:
>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>



Nov 19 '05 #4

P: n/a
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...
"Scott Allen" wrote:
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 29 Mar 2005 07:11:02 -0800, "Tim::.." <myatix_at_hotmail.com>
wrote:
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

"Scott Allen" wrote:
Hi Tim:

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

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 28 Mar 2005 23:59:03 -0800, "Tim::.." <myatix_at_hotmail.com>
wrote:

>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>


Nov 19 '05 #5

P: n/a
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 :)

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 29 Mar 2005 07:43:03 -0800, "Tim::.." <myatix_at_hotmail.com>
wrote:
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...


Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.