Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 952
  • Last Modified:

Retrieve DisplayName using directorySearcher

How can I grab the DisplayName from AD using that class?
0
NickMalloy
Asked:
NickMalloy
1 Solution
 
fizchCommented:
Ok, I really can't explain too much of this as it was already in production once I started here. However, I have reused this code several times and it does work well. It is going to have more information than what you were requesting, but you can always remove the excess.

Public Function GetUser(ByVal UserId As String)
            Dim adEntry As New DirectoryEntry(_connStr, _LDAPuserid, _LDAPpassword)

            'adEntry.AuthenticationType = AuthenticationTypes.Anonymous

            Dim adSearcher As New DirectorySearcher(adEntry)

            If UserId.Trim.Length = 0 Then
                _userid = "Userid error"
                _status = False
                Return 0
            End If

            adSearcher.Filter = "(UID=" & UserId.ToString & ")"
            adSearcher.PageSize = 1
            adSearcher.PropertiesToLoad.Add("UID")
            adSearcher.PropertiesToLoad.Add("cn")
            adSearcher.PropertiesToLoad.Add("mail")
            adSearcher.PropertiesToLoad.Add("telephoneNumber")

            Dim adResult As SearchResult

            Try
                adResult = adSearcher.FindOne
                If Not (adResult Is Nothing) Then
                    If (adResult.Properties.Contains("UID")) Then
                        _userid = adResult.Properties("UID")(0).ToString()
                    End If

                    If (adResult.Properties.Contains("cn")) Then
                        _name = adResult.Properties("cn")(0).ToString()
                    End If

                    If (adResult.Properties.Contains("mail")) Then
                        _email = adResult.Properties("mail")(0).ToString()
                    End If

                    If (adResult.Properties.Contains("telephoneNumber")) Then
                        _phone = adResult.Properties("telephoneNumber")(0).ToString()
                    End If
                    _status = True
                Else
                    _userid = ""
                    _name = ""
                    _email = ""
                    _phone = ""
                    _status = False
                End If
            Catch ex As Runtime.InteropServices.COMException
                _userid = ""
                _name = "Server Error"
                _email = ""
                _phone = ""
                _status = False
            End Try

            Return 0
        End Function
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now