Solved

Get DisplayName for User's Manager from AD distinguishedName

Posted on 2016-10-04
2
40 Views
Last Modified: 2016-10-04
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

0
Comment
Question by:bjblackmore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Accepted Solution

by:
bjblackmore earned 0 total points
ID: 41828176
Added the below looks to have worked:

            Using user As New DirectoryEntry("LDAP://" & distinguishedName)
                Dim strMgrDN As String = GetADProperty(user, "manager")
                If Not strMgrDN = "" Then
                    Using Manager As New DirectoryEntry("LDAP://" & strMgrDN)
                        managerDN.Text = GetADProperty(Manager, "displayName")
                    End Using
                Else
                    managerDN.Text = "Empty"
                End If
            End Using
0
 

Author Closing Comment

by:bjblackmore
ID: 41828253
Resolved on my own
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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