• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1442
  • Last Modified:

Getting a list of Direct Reports from Active Directory (vb)

Hey Everyone!

I know how to get a list from AD showing group affiliations and it works just great!  I need to do something else very similar - I need to generate a list of a person's Direct Reports from the Organization tab in Active Directory.  Does anyone know how to do that?

Thank you in advance!!

wgarneau
0
wgarneau
Asked:
wgarneau
  • 5
  • 3
1 Solution
 
ihenryCommented:
It'd look like the following

using ( SearchResultCollection users = container.FindAll() )
{
      foreach ( SearchResult result in results )
      {
            if ( result.Contains("directReports") )
            {
                  if ( result.Properties["directReports"].Count > 0 )
                  {
                        for ( int i=0; i<result.Properties["directReports"].Count; i++ )
                        {
                              'u is distinguishedName of a user which directly report to this user
                              String u = (String) result.Properties["directReports"][i];
                        }
                  }
            }
      }
}
0
 
ihenryCommented:
Oops..didn't read carefully your question. are you able to convert it to vb.net yourself?
0
 
wgarneauAuthor Commented:
ihenry,

I'm using VB.  I tried a VB variation of what you have though:

Public Function GetDirectReports() As String
Dim search As DirectorySearcher = New DirectorySearcher(_path)
            search.Filter = "(cn=" & _filterAttribute & ")"
            search.PropertiesToLoad.Add("memberOf")
            Dim dReportNames As StringBuilder = New StringBuilder

            Try
                Dim result As SearchResult = search.FindOne()
                Dim propertyCount As Integer = result.Properties("directReports").Count

                Dim dn As String
                Dim equalsIndex, commaIndex

                Dim propertyCounter As Integer

                For propertyCounter = 0 To propertyCount - 1
                    dn = CType(result.Properties("directReports")(propertyCounter), String)

                    equalsIndex = dn.IndexOf("=", 1)
                    commaIndex = dn.IndexOf(",", 1)
                    If (equalsIndex = -1) Then
                        Return Nothing
                    End If

                    dReportNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1))
                    dReportNames.Append("|")
                Next

            Catch ex As Exception
                Throw New Exception("Error obtaining list of Direct Reports. " & ex.Message)
            End Try

            Return dReportNames.ToString()
End Function

I get the following:

Object reference not set to an instance of an object.

The line generating the error is this one:

Dim propertyCount As Integer = result.Properties("directReports").Count

wgarneau
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
wgarneauAuthor Commented:
Boy - we must have posted at the same time!

It looks to me like directReports isn't the correct property name in AD...

...but then if I knew, I wouldn't need help LOL!!

wgarneau
0
 
ihenryCommented:
Are you sure the search is returning something? FindOne method will return null or nothing if nothing match the criteria. And also you should narrow down your search criteria if you're searching only for user objects, like this
(&(objectClass=user)(objectCategory=person)(cn=aName))
(&(objectClass=user)(objectCategory=person)(sAMAccountName=aName))
0
 
wgarneauAuthor Commented:
OK I got it..

Dumb mistake - I changed:

search.PropertiesToLoad.Add("memberOf")

to:

search.PropertiesToLoad.Add("directReports")

That works great - though I'd rather have the logon rather than the full name.  How hard is it to look-up a logon name from a full name?

wgarneau
0
 
ihenryCommented:
I'm not sure if directReports attribute is a calculated attribute or not, if yes it might not being returned by default. But you can try this

Dim searcher As New DirectorySearcher()
searcher.PropertiesToLoad.Add( "directReports" )
...
...

If result.Contains("directReports") Then
      For i As Integer = 0 To result.Properties["directReports"].Count )
            'u is distinguishedName of a user which directly report to this user
             Dim u As String = CType( result.Properties["directReports"][i], String )
      Next
End If
0
 
ihenryCommented:
Ok, you got it worked.

>> How hard is it to look-up a logon name
You just need to make a small modification like I have posted above, the rest are the same.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now