none
Access properties such as Job Title from Active Directory RRS feed

  • Question

  • I am working on a Windows form application where I need to display the current logged in user's information.  Name, Display Name, are easy to get through System.DirectoryServices.AccountManagement.UserPrincipal.Current, but is there a way to get to the other properties in the AD just as Job title?

    Thanks


    Brad Allison

    Thursday, May 2, 2019 2:08 PM

Answers

  • Here is a BackGroundWorker routine that will give you most if not everything you would want from AD

    Private Sub LoadAD_BGW_DoWork(Sender As Object, e As DoWorkEventArgs) Handles LoadAD_BGW.DoWork
    
            Dim DT As New DataTable With {.TableName = "ActiveDirectory"}
    
            Using ADSearcher As New DirectorySearcher()
                With ADSearcher
                    .Filter = "(&(|(samAccountType=805306368)))"
                    .PropertiesToLoad.Add("EmployeeID")
                    .PropertiesToLoad.Add("physicalDeliveryOfficeName")
                    .PropertiesToLoad.Add("departmentNumber")
                    .PropertiesToLoad.Add("department")
                    .PropertiesToLoad.Add("title")
                    .PropertiesToLoad.Add("employeeType")
                    .PropertiesToLoad.Add("givenName")
                    .PropertiesToLoad.Add("sn")
                    .PropertiesToLoad.Add("manager")
                    .PropertiesToLoad.Add("telephoneNumber")
                    .PropertiesToLoad.Add("streetAddress")
                    .PropertiesToLoad.Add("l")
                    .PropertiesToLoad.Add("postalCode")
                    .PropertiesToLoad.Add("st")
                    .PropertiesToLoad.Add("co")
                    .PropertiesToLoad.Add("mobile")
                    .PropertiesToLoad.Add("Description")
                    .PropertiesToLoad.Add("sAMAccountname")
                    .PropertiesToLoad.Add("DistinguishedName")
                    .PropertiesToLoad.Add("mail")
                    .PropertiesToLoad.Add("company")
                    .PropertiesToLoad.Add("ADsPath")
                    .PropertiesToLoad.Add("initials")
                    .PropertiesToLoad.Add("userAccountControl")
                    .PageSize = 2000
                End With
                Dim ADResults As SearchResultCollection
                ADResults = ADSearcher.FindAll
    
                For Each PropertyName As String In ADSearcher.PropertiesToLoad
                    DT.Columns.Add(PropertyName, GetType(String))
                Next
                DT.Columns.Add("Select", GetType(Boolean))
                DT.Columns("Select").DefaultValue = False            
                DT.Columns.Add("IsDisabled", GetType(Boolean))
                DT.Columns.Add("StatusNotes", GetType(String))
                DT.Columns.Add("FullName", GetType(String), "givenName + ' ' + sn")
                DT.Columns("IsDisabled").DefaultValue = False
                DT.Columns("IsDisabled").Expression = "IIF(userAccountControl='512',0,IIF(userAccountControl='514',1," &
                    "IIF(userAccountControl='546',1,IIF(userAccountControl='66048',0,IIF(userAccountControl='66050',0," &
                    "IIF(userAccountControl='66080',0,''))))))"
    
                DT.Columns("StatusNotes").Expression = "IIF(userAccountControl='512','Normal Account',IIF(userAccountControl='514','Disabled Account'," &
                    "IIF(userAccountControl='546','Password Not Required',IIF(userAccountControl='66048','Password Doesnt Expire',IIF(userAccountControl='66050','Password Doesnt Expire'," &
                    "IIF(userAccountControl='66080','Password Not Required',''))))))"
    
    
                Dim ADResult As SearchResult
    
                Dim Results As Integer = ADResults.Count
                Dim i As Integer = 0
    
                For Each ADResult In ADResults
    
                    Dim dr As DataRow = DT.NewRow()
                    For Each ColName As String In ADSearcher.PropertiesToLoad
    
                        If ADResult.Properties.Contains(ColName) Then
    
                            dr(ColName) = CStr(ADResult.Properties(ColName)(0))
                        Else
                            dr(ColName) = ""
                        End If
                    Next
                    DT.Rows.Add(dr)
                    LoadAD_BGW.ReportProgress(CInt(100 * i / Results))
                    i += 1
                Next
                For Each row As DataRow In DT.Rows
                    row("employeeID") = row("employeeID").ToString().PadLeft(5, "0"c)
                Next
                ADResults.Dispose()
                e.Result = DT
            End Using
        End Sub


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, May 2, 2019 2:37 PM
  • And one quick question about this.  I dont really need a table of everyone, but the current signed in user.  How do I filter for only the current user?

    Brad Allison

    There are a few ways to do that.

    Get the username (or domain\username) from Environment and pass it to the filter, it should looks something like this:

    "(&(objectCategory=person)(objectClass=user)(cn=UserName))"

    Though dont quote me on it


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, May 2, 2019 3:47 PM

All replies

  • Here is a BackGroundWorker routine that will give you most if not everything you would want from AD

    Private Sub LoadAD_BGW_DoWork(Sender As Object, e As DoWorkEventArgs) Handles LoadAD_BGW.DoWork
    
            Dim DT As New DataTable With {.TableName = "ActiveDirectory"}
    
            Using ADSearcher As New DirectorySearcher()
                With ADSearcher
                    .Filter = "(&(|(samAccountType=805306368)))"
                    .PropertiesToLoad.Add("EmployeeID")
                    .PropertiesToLoad.Add("physicalDeliveryOfficeName")
                    .PropertiesToLoad.Add("departmentNumber")
                    .PropertiesToLoad.Add("department")
                    .PropertiesToLoad.Add("title")
                    .PropertiesToLoad.Add("employeeType")
                    .PropertiesToLoad.Add("givenName")
                    .PropertiesToLoad.Add("sn")
                    .PropertiesToLoad.Add("manager")
                    .PropertiesToLoad.Add("telephoneNumber")
                    .PropertiesToLoad.Add("streetAddress")
                    .PropertiesToLoad.Add("l")
                    .PropertiesToLoad.Add("postalCode")
                    .PropertiesToLoad.Add("st")
                    .PropertiesToLoad.Add("co")
                    .PropertiesToLoad.Add("mobile")
                    .PropertiesToLoad.Add("Description")
                    .PropertiesToLoad.Add("sAMAccountname")
                    .PropertiesToLoad.Add("DistinguishedName")
                    .PropertiesToLoad.Add("mail")
                    .PropertiesToLoad.Add("company")
                    .PropertiesToLoad.Add("ADsPath")
                    .PropertiesToLoad.Add("initials")
                    .PropertiesToLoad.Add("userAccountControl")
                    .PageSize = 2000
                End With
                Dim ADResults As SearchResultCollection
                ADResults = ADSearcher.FindAll
    
                For Each PropertyName As String In ADSearcher.PropertiesToLoad
                    DT.Columns.Add(PropertyName, GetType(String))
                Next
                DT.Columns.Add("Select", GetType(Boolean))
                DT.Columns("Select").DefaultValue = False            
                DT.Columns.Add("IsDisabled", GetType(Boolean))
                DT.Columns.Add("StatusNotes", GetType(String))
                DT.Columns.Add("FullName", GetType(String), "givenName + ' ' + sn")
                DT.Columns("IsDisabled").DefaultValue = False
                DT.Columns("IsDisabled").Expression = "IIF(userAccountControl='512',0,IIF(userAccountControl='514',1," &
                    "IIF(userAccountControl='546',1,IIF(userAccountControl='66048',0,IIF(userAccountControl='66050',0," &
                    "IIF(userAccountControl='66080',0,''))))))"
    
                DT.Columns("StatusNotes").Expression = "IIF(userAccountControl='512','Normal Account',IIF(userAccountControl='514','Disabled Account'," &
                    "IIF(userAccountControl='546','Password Not Required',IIF(userAccountControl='66048','Password Doesnt Expire',IIF(userAccountControl='66050','Password Doesnt Expire'," &
                    "IIF(userAccountControl='66080','Password Not Required',''))))))"
    
    
                Dim ADResult As SearchResult
    
                Dim Results As Integer = ADResults.Count
                Dim i As Integer = 0
    
                For Each ADResult In ADResults
    
                    Dim dr As DataRow = DT.NewRow()
                    For Each ColName As String In ADSearcher.PropertiesToLoad
    
                        If ADResult.Properties.Contains(ColName) Then
    
                            dr(ColName) = CStr(ADResult.Properties(ColName)(0))
                        Else
                            dr(ColName) = ""
                        End If
                    Next
                    DT.Rows.Add(dr)
                    LoadAD_BGW.ReportProgress(CInt(100 * i / Results))
                    i += 1
                Next
                For Each row As DataRow In DT.Rows
                    row("employeeID") = row("employeeID").ToString().PadLeft(5, "0"c)
                Next
                ADResults.Dispose()
                e.Result = DT
            End Using
        End Sub


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, May 2, 2019 2:37 PM
  • Thanks for this

    Brad Allison

    Thursday, May 2, 2019 2:38 PM
  • And one quick question about this.  I dont really need a table of everyone, but the current signed in user.  How do I filter for only the current user?

    Brad Allison

    Thursday, May 2, 2019 3:28 PM
  • And one quick question about this.  I dont really need a table of everyone, but the current signed in user.  How do I filter for only the current user?

    Brad Allison

    There are a few ways to do that.

    Get the username (or domain\username) from Environment and pass it to the filter, it should looks something like this:

    "(&(objectCategory=person)(objectClass=user)(cn=UserName))"

    Though dont quote me on it


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, May 2, 2019 3:47 PM