Link to home
Start Free TrialLog in
Avatar of gowerman
gowerman

asked on

AD connection get mail properties problem

Hi,

VB, VS 2010express,

Im trying to get email properties from AD, when I do

 Public Shared Sub ADconnection()

        Dim oSearcher As New DirectorySearcher
        Dim oResults As SearchResultCollection
        Dim oResult As SearchResult
        Dim mCount As Integer
        Dim mLDAPRecord, mLDAPemail, mLDAPFullname As String

        Dim ResultFields() As String = {"securityEquals", "cn"}

        Try
            With oSearcher
                .SearchRoot = New DirectoryEntry("LDAP://" & "domainserver" & _
          "/dc=xxx,dc=local")
                .PropertiesToLoad.AddRange(ResultFields)
                .Filter = "sn=" & "*"
                oResults = .FindAll()
            End With

            mCount = oResults.Count
            If mCount > 0 Then
                For Each oResult In oResults
                    mLDAPRecord = oResult.GetDirectoryEntry().Properties("cn").Value
                    mLDAPemail = oResult.GetDirectoryEntry().Properties("mail").Value
                    mLDAPFullname = oResult.GetDirectoryEntry().Properties("sn").Value
                   
                Next
            End If
        Catch e As Exception

            MsgBox("Error is " & e.Message)

        End Try

    End Sub

Open in new window



then everything is working but if I change filter="cn=username" then even if he find a record it's not taking email address
any reason why?

thanks for all advise
regards
Avatar of Tasmant
Tasmant
Flag of France image

could it not be possible that your recordset don't contain the expected property?
Dim ResultFields() As String = {"securityEquals", "cn"}
I would change it to:
Dim ResultFields() As String = {"securityEquals", "cn", "mail"}
and maybe more to:
Dim ResultFields() As String = {"securityEquals", "cn", "mail", "sn"}

Avatar of gowerman
gowerman

ASKER

i change as you said but still same. I checked several records they all contain that property as when I run the first one it takes all users emails
this attribute shouldn't be a multivalued attribute, but i've found some code
mLDAPemail = oResult.GetDirectoryEntry().Properties("mail")(0).toString()
mLDAPemail = oResult.GetDirectoryEntry().Properties("mail")(0).Value
maybe can you try ...
when I tried this then I have index is out of range
ok so this is singlevalued, that's fine. Don't know why you encounter the issue, i'll try to investigate further.
ASKER CERTIFIED SOLUTION
Avatar of gowerman
gowerman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thats works for me