ADSI Cleanup Help!




Can someone please tell me how I can combine the function and the sub below
to create a cleaner bit of code! I want to loop through the ActiveDirectory
and return users information in a datagrid!

Thanks for any help!

Sub GetUserADInfo()
Dim myDirectory As DirectoryEntry = New DirectoryEntry("LDAP://" +
ConfigurationSettings.AppSettings.Get("System_ActiveDirPath") )
' Create a DirectorySearcher object.
Dim mySearcher As New DirectorySearcher(Entry)
' Use the FindOne method to find the object, which in this case, is
the user
' indicated by User Name and assign it to a SearchResult.
mySearcher.Filter = ("(&(objectCategory=person)(objectClass=user))")
'Dim MySearchResult As SearchResult = mySearcher.FindOne
Dim results As SearchResultCollection

results = mySearcher.FindAll()
dgADUserInfo.DataSource = results

End Sub

Public Function GetUserInfo(ByVal inType As String) As String

Dim sPath As String = "LDAP://"
Dim myDirectory As DirectoryEntry = New DirectoryEntry("LDAP://"
+ ConfigurationSettings.AppSettings.Get("System_ActiveDirPath") )
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))")

' Dim i As Int32 = -1
For Each mySearchResultColl In mySearcher.FindAll()

' i += 1

'I expect only one user from search result
Select Case mySearchResultColl.Count
Case 0
Return "Null"
Exit Function
Case Is < 1
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))
' Next
Catch ex As System.Exception

'do some error return here.
End Try
End Function


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