Solved

Get DisplayName for User's Manager from AD distinguishedName

Posted on 2016-10-04
2
27 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.NET Error 7 42
How Can I fix my MouseHover Event? 14 27
asp.net bundle 8 34
Windows Service to Receive TCP Packets 4 33
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

13 Experts available now in Live!

Get 1:1 Help Now