Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with DirectorySearcher in LDAP

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I am new to this. We have a "old" list of LDAP fields and I want to list what fields we currently have. I have this code and when it gets to a field we don't have it errors.

Can someone help me modify this to create a loop and list all of the fields in our LDAP?
Then I can modify the >>> oSearchResult.Properties("XXXX")(0).ToString()
to match ours
TIA
Code:
  Public Function getEmployeeInfo(ByVal strEid As String) As Employee      
Dim newEmployee As Employee = New Employee
        Dim oDirectoryEntry As DirectoryEntry = New DirectoryEntry(strServerName, strUserName, strPasswordIn, AuthenticationTypes.None)
        Dim oDirectorySearcher As DirectorySearcher = New DirectorySearcher(oDirectoryEntry)
        oDirectoryEntry.Dispose()
        oDirectorySearcher.Filter = String.Format("(vzeid={0})", strEid)
        Dim oSearchResult As SearchResult = oDirectorySearcher.FindOne()
        If Not (oSearchResult Is Nothing) Then
            newEmployee.strEid = strEid
            newEmployee.strVzid = oSearchResult.Properties("vzid")(0).ToString()
            newEmployee.strName = oSearchResult.Properties("cn")(0).ToString()
            newEmployee.strEmail = oSearchResult.Properties("mail")(0).ToString()
   ' lots more fields here ...
       End If
       oDirectorySearcher.Dispose()
        Return newEmployee
   End Function

DougP
 


You can itterate through the Property Collection:

Code:
Dim propCol As ResultPropertyCollection
For Each result In oSearchResult 
    propCol = result.properties
    For Each strKey In propCol.PropertyNames
        Debug.Print(strKey)
    Next
Next



Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Ok I get a couple of blue squiggly lines.
on the oSearchResult says
Expression is of type 'System.DirectoryServices.SearchResult', which is not a collection type.
Any ideas?
Code:
       Dim oDirectoryEntry As DirectoryEntry = New DirectoryEntry(strServerName, strUserName, strPasswordIn, AuthenticationTypes.None)
        Dim oDirectorySearcher As DirectorySearcher = New DirectorySearcher(oDirectoryEntry)
        oDirectoryEntry.Dispose()

        Dim oSearchResult As SearchResult = oDirectorySearcher.FindOne()

        Dim propCol As ResultPropertyCollection
        For Each result In [highlight #FCE94F][b]oSearchResult[/b][/highlight] 
           propCol = result.properties
            For Each strKey In propCol.PropertyNames
                Debug.Print(strKey)
            Next
        Next

DougP
 
I used it with FindAll, but it should work with FindOne as well. I also added it to a List so I can view it easier

Code:
Dim searchResults As [red]SearchResultCollection[/red] = searcher.FindAll
Dim propCol As ResultPropertyCollection
Dim s As New List(Of String)
For Each result In searchResults
    propCol = result.properties
    For Each strKey In propCol.PropertyNames
        s.Add(strKey & ": " & SearchResultProperty(search, strKey))
    Next
Next


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
this is .NET 4.0 BTW. so searcher had to be changed and then it still did not work so I just changed SearchResult to SearchResultCollection
as in your post above. Now I get blue squiggly on [highlight #729FCF]oDirectorySearcher[/highlight]
ERROR >>>> Error 1 Value of type System.DirectoryServices.SearchResult' cannot be converted to 'System.DirectoryServices.SearchResultCollection
Code:
Dim oDirectoryEntry As DirectoryEntry = New DirectoryEntry(strServerName, strUserName, strPasswordIn, AuthenticationTypes.None)
Dim oDirectorySearcher As DirectorySearcher = New DirectorySearcher(oDirectoryEntry)
oDirectoryEntry.Dispose()


Dim oSearchResult As SearchResultCollection = [highlight #729FCF]oDirectorySearcher[/highlight].FindOne()

        Dim propCol As ResultPropertyCollection
        For Each result In oSearchResult
            propCol = result.properties
            For Each strKey In propCol.PropertyNames
                Debug.Print(strKey)
            Next
        Next

DougP
 
FWI: I found this code but it only returns 3 lines. But I want the attributes for eveything not just people. and if I change .FindOne to .FindAll get blue squiggly again on oDirectorySearcher.FindOne()
3 lines returned:
adspath=LDAP://ldapfdc.xxxx.com:1391/ou=People,o=xxxx.com
ou=People
objectclass=top

Code:
       Dim oDirectoryEntry As DirectoryEntry = New DirectoryEntry(strServerName, strUserName, strPasswordIn, AuthenticationTypes.None)
        Dim oDirectorySearcher As DirectorySearcher = New DirectorySearcher(oDirectoryEntry)
        oDirectoryEntry.Dispose()
       Dim oSearchResult As SearchResult = oDirectorySearcher.FindOne()
        Dim i As Integer = 0
        Do Until i >= oSearchResult.Properties.PropertyNames.Count
            Debug.Print(oSearchResult.Properties.PropertyNames(i).ToString() & "=" & _
                        oSearchResult.Properties.Values(i)(0).ToString())
            i += 1
        Loop

DougP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top