Retrieve DisplayName using directorySearcher

Posted on 2006-05-22
Last Modified: 2008-01-09
How can I grab the DisplayName from AD using that class?
Question by:NickMalloy
    1 Comment
    LVL 6

    Accepted Solution

    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

                Dim adResult As SearchResult

                    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
                        _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

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now