R
Rob Meade
Hi all,
I have some code which I cobbled together from some examples online, so its
probably wrong, although I did have it working a little while ago,
unfortunately for some reason it seems to have stopped working..nothing's
change infrastructure wise, the only thing that has changed is that my
project has been converted to VS2005 - I'm not suggesting this has caused a
problem - just stating its the only change (that I can remember!)...
The code (snipped from the class):
Public Sub Search(ByVal samaccountname As String, ByVal activeDirectory As
String)
' declare variables
Dim DirectoryEntry As DirectoryEntry = New
DirectoryEntry(LDAP://myActiveDirectory)
Dim DirectorySearcher As DirectorySearcher = New
DirectorySearcher(DirectoryEntry)
Dim Results As DirectoryServices.SearchResultCollection
Dim Result As DirectoryServices.SearchResult
Dim ResultPropertyCollection As ResultPropertyCollection
Dim ResultProperty As String
' define search properties
With (DirectorySearcher)
.Filter =
"(&(objectClass=User)(objectCategory=User)(samaccountname=" & samaccountname
& "))"
.SearchScope = SearchScope.Subtree
End With
' search
Results = DirectorySearcher.FindAll
' check number of results
If Results.Count > 0 Then
' iterate through results
For Each Result In Results
' exception handling
Try
' populate collection
ResultPropertyCollection = Result.Properties
' set class variables
Me.SetRawData(RawData)
' exception handling
Try
Me.SetGivenName(Result.Properties("givenname").Item(0).ToString)
Catch ex As System.NullReferenceException
Me.SetGivenName("")
End Try
' exception handling
Try
Me.SetSurname(Result.Properties("sn").Item(0).ToString)
Catch ex As System.NullReferenceException
Me.SetSurname("")
End Try
End If
End Sub
This is a cut down version of what I'm currently using, but only in that I
was pulling more properties out..
Can anyone spot anything which would prevent the search from working? I'm
not getting any errors, but equally I'm not getting any values out of the ad
either - really frustrating as I was dead impressed with myself when I had
it working originally! )
Also, I'm sure this is nothing to do with it but are there any specific
ports that would need to be open on a LAN to enable this? I'd imagine I'd
have received an error somewhere if so - and I'm pretty sure that there
haven't been any infrastructure changes since it was working..
Regards
Rob
I have some code which I cobbled together from some examples online, so its
probably wrong, although I did have it working a little while ago,
unfortunately for some reason it seems to have stopped working..nothing's
change infrastructure wise, the only thing that has changed is that my
project has been converted to VS2005 - I'm not suggesting this has caused a
problem - just stating its the only change (that I can remember!)...
The code (snipped from the class):
Public Sub Search(ByVal samaccountname As String, ByVal activeDirectory As
String)
' declare variables
Dim DirectoryEntry As DirectoryEntry = New
DirectoryEntry(LDAP://myActiveDirectory)
Dim DirectorySearcher As DirectorySearcher = New
DirectorySearcher(DirectoryEntry)
Dim Results As DirectoryServices.SearchResultCollection
Dim Result As DirectoryServices.SearchResult
Dim ResultPropertyCollection As ResultPropertyCollection
Dim ResultProperty As String
' define search properties
With (DirectorySearcher)
.Filter =
"(&(objectClass=User)(objectCategory=User)(samaccountname=" & samaccountname
& "))"
.SearchScope = SearchScope.Subtree
End With
' search
Results = DirectorySearcher.FindAll
' check number of results
If Results.Count > 0 Then
' iterate through results
For Each Result In Results
' exception handling
Try
' populate collection
ResultPropertyCollection = Result.Properties
' set class variables
Me.SetRawData(RawData)
' exception handling
Try
Me.SetGivenName(Result.Properties("givenname").Item(0).ToString)
Catch ex As System.NullReferenceException
Me.SetGivenName("")
End Try
' exception handling
Try
Me.SetSurname(Result.Properties("sn").Item(0).ToString)
Catch ex As System.NullReferenceException
Me.SetSurname("")
End Try
End If
End Sub
This is a cut down version of what I'm currently using, but only in that I
was pulling more properties out..
Can anyone spot anything which would prevent the search from working? I'm
not getting any errors, but equally I'm not getting any values out of the ad
either - really frustrating as I was dead impressed with myself when I had
it working originally! )
Also, I'm sure this is nothing to do with it but are there any specific
ports that would need to be open on a LAN to enable this? I'd imagine I'd
have received an error somewhere if so - and I'm pretty sure that there
haven't been any infrastructure changes since it was working..
Regards
Rob