Link to home
Start Free TrialLog in
Avatar of bjblackmore
bjblackmore

asked on

Get DisplayName for User's Manager from AD distinguishedName

I'm using the below code to pull a number of attributes from a users AD profile. I can get the user's manager's distinguishedName, but I want to display the displayName. How can i either pull the displayName for the manager form AD, using either another directory query, or by splitting the distinguishedName into just the name, and getting rid of the rest of the string?

I get:
manager=CN=Bloggs\, Joe,OU=IT,OU=Users,DC=domain,DC=net
I want:
manager="Joe Bloggs" or "Bloggs, Joe"

Private Function GetUserProperties() As ADProperties
        Dim ADName As String = GetLogonName()
        Dim bSuccess As Boolean = False
        Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
        Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
        Dim waitTime As TimeSpan = New TimeSpan(0, 0, 0, 5, 0)
        dirSearcher.ClientTimeout = waitTime
        dirSearcher.Filter = ("(samAccountName=" & ADName & ")")
        dirSearcher.PropertiesToLoad.Add("manager")
        dirSearcher.SearchScope = SearchScope.Subtree
        Try
            Dim dirResult As SearchResult = dirSearcher.FindOne()
            bSuccess = Not (dirResult Is Nothing)
            If dirResult.GetDirectoryEntry.Properties("manager").Value Is Nothing Then
                GetUserProperties.manager = "<Not Set>"
            Else
                GetUserProperties.manager = (dirResult.Properties("manager")(0).ToString())
            End If
            bSuccess = True
        Catch ex As Exception
            bSuccess = False
            MsgBox("No Connection to the domain." & Environment.NewLine & "Please connect to corporate network & try again.", MsgBoxStyle.Critical, "Network Error #1")
            Application.Exit()
        End Try
    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of bjblackmore
bjblackmore

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
Avatar of bjblackmore
bjblackmore

ASKER

Resolved on my own