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.Propertie s("defaultNamin gContext").Valu e
Dim Results As SearchResultCol lection, 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 DirectorySearch er(Entry)
With Searcher
.SearchScope = SearchScope.Sub tree
.Filter = "(&(objectClass =person)(sAMAcc ountName=" & UserName &
"))"
'Only works when threaded. Keep this in mind.
'Default size returned is 1000 (server set)
.PageSize = 100
.PropertiesToLo ad.AddRange(New String() {"sAMAccountNam e",
"displayNam e"})
Result = .FindOne
End With
If Not Result Is Nothing Then
'Watch for multivalued properties
If TypeOf Result.GetDirec toryEntry.Prope rties("displayN ame") Is
System.Array Then
MessageBox.Show (Result.GetDire ctoryEntry.Prop erties("display Name")(0).Value )
Else
MessageBox.Show (Result.GetDire ctoryEntry.Prop erties("display Name").Value)
End If
End If
With Searcher
'You can use wild cards, Look up the RFC on LDAP for more info
.Filter = "(&(objectClass =person)(sAMAcc ountName=" & UserName &
"*))"
Results = Searcher.FindAl l
End With
For Each Result In Results
'Same as before
If TypeOf Result.GetDirec toryEntry.Prope rties("displayN ame") Is
System.Array Then
Console.WriteLi ne(Result.GetDi rectoryEntry.Pr operties("displ ayName")(0).Val ue)
Else
Console.WriteLi ne(Result.GetDi rectoryEntry.Pr operties("displ ayName").Value)
End If
Next
End Sub
"Phil Kelly" <ph********@inf atech.com> wrote in message
news:cb******** **@sparta.btint ernet.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=User s,DC=Test,DC=lo cal) 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=loca l")
Dim myRepDirectoryS earcher As New DirectorySearch er(mySearchRoot )
myRepDirectoryS earcher.Filter = objectName
' Get the first entry of the search.
Dim myRepSearchResu lt As SearchResult = myRepDirectoryS earcher.FindOne ()
MsgBox(myRepDir ectorySearcher. Filter)
If Not (myRepSearchRes ult Is Nothing) Then
' Get the 'DirectoryEntry ' that corresponds to 'myRepSearchRes ult'.
Dim myDirectoryEntr y As DirectoryEntry =
myRepSearchResu lt.GetDirectory Entry()
MsgBox(myDirect oryEntry.Name)
Dim myRepSearchResu ltPath As String = myRepSearchResu lt.Path
MsgBox(myRepSea rchResultPath)
End If
End Sub