Phill,
I hope this helps, I haven't tested this exact code, it should work
though. I have a simple control inherited from treeview that lets you get
the search root. Just drop it on your form, call one method and it will let
you select and OU or container. If you are interested let me know.
Jared
Sub doRep(ByVal UserName As String) 'User name - NOT display name
Dim Entry As New DirectoryEntry("LDAP://RootDSE")
Dim DomainPath As String = "LDAP://" &
Entry.Properties("defaultNamingContext").Value
Dim Results As SearchResultCollection, Result As SearchResult
'Domain Naming Context
'DC=MyDomain,DC=com
Entry = New DirectoryEntry(DomainPath)
'I've tried to set the path directly but it is empty, so I started
using a path variable
Dim Searcher As New DirectorySearcher(Entry)
With Searcher
.SearchScope = SearchScope.Subtree
.Filter = "(&(objectClass=person)(sAMAccountName=" & UserName &
"))"
'Only works when threaded. Keep this in mind.
'Default size returned is 1000 (server set)
.PageSize = 100
.PropertiesToLoad.AddRange(New String() {"sAMAccountName",
"displayName"})
Result = .FindOne
End With
If Not Result Is Nothing Then
'Watch for multivalued properties
If TypeOf Result.GetDirectoryEntry.Properties("displayName") Is
System.Array Then
MessageBox.Show(Result.GetDirectoryEntry.Propertie s("displayName")(0).Value)
Else
MessageBox.Show(Result.GetDirectoryEntry.Propertie s("displayName").Value)
End If
End If
With Searcher
'You can use wild cards, Look up the RFC on LDAP for more info
.Filter = "(&(objectClass=person)(sAMAccountName=" & UserName &
"*))"
Results = Searcher.FindAll
End With
For Each Result In Results
'Same as before
If TypeOf Result.GetDirectoryEntry.Properties("displayName") Is
System.Array Then
Console.WriteLine(Result.GetDirectoryEntry.Propert ies("displayName")(0).Value)
Else
Console.WriteLine(Result.GetDirectoryEntry.Propert ies("displayName").Value)
End If
Next
End Sub
"Phil Kelly" <ph********@infatech.com> wrote in message
news:cb**********@sparta.btinternet.com...
Hi!
I hope someone can help me here because I'm tearing my hair out (what
little
there is of it!) trying to figure out what's going on with the code below.
I'm passing an Active Directory CN of a user object (like
CN=Phil,OU=Users,DC=Test,DC=local) to the doRep() function, then have the
function search for the user in AD ('FindOne')
Then, I'm trying to get the code to msgbox the directory entry name....
but
we never seem to get that far; nothing turns up in a msgbox
Now, I'm not that au fait with vb .NET but I'm finding the challenge to be
superb. I really hope someone will show me the error of my ways...!
Thanks
Phil
Public Sub doRep(ByVal objectName As String)
' Create a 'DirectoryEntry' object to search.
Dim mySearchRoot As New DirectoryEntry("LDAP://DC=test,DC=local")
Dim myRepDirectorySearcher As New DirectorySearcher(mySearchRoot)
myRepDirectorySearcher.Filter = objectName
' Get the first entry of the search.
Dim myRepSearchResult As SearchResult = myRepDirectorySearcher.FindOne()
MsgBox(myRepDirectorySearcher.Filter)
If Not (myRepSearchResult Is Nothing) Then
' Get the 'DirectoryEntry' that corresponds to 'myRepSearchResult'.
Dim myDirectoryEntry As DirectoryEntry =
myRepSearchResult.GetDirectoryEntry()
MsgBox(myDirectoryEntry.Name)
Dim myRepSearchResultPath As String = myRepSearchResult.Path
MsgBox(myRepSearchResultPath)
End If
End Sub