Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining a list of direct reports from the current user

I have checked the two other threads and even used code from one but it never populates a list. When I open up Active Directory Users and Computers and go to my Manager under Organization I see his list of direct reports.

What I am trying to do is gain access to that list through code. Nothing I have found so far seems to work.

    public void GetDirectoryEntry(string adUserName)
    {
        DirectoryEntry de = new DirectoryEntry("LDAP://server.server.com");

        DirectorySearcher ds = new DirectorySearcher(de);
        ds.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=" + adUserName + "))";
        ds.SearchScope = SearchScope.Subtree;

        SearchResult rs = ds.FindOne();

        string distinguishedName = rs.Properties["distinguishedName"][0].ToString();
        string department = rs.Properties["department"][0].ToString();
        string manager = rs.Properties["manager"][0].ToString();
        //string temp3 = rs.Properties["Reports"][0].ToString();

    }

I have tried using Reports and directReports and neither work both error out.

This method loads up the logged in user or any user I pass into it. I can access all of their properties but I cannot seem to get access to their direct reports.

What am I missing?

Found the answer:

foreach (string objProperty in rs.Properties["DirectReports"])
            {
                isManager = true;
                string emp = objProperty.ToString();
                string[] setp = new string[1];
                setp[0] = "DC"; //If your users are in a OU use OU 

                emp = emp.Split(setp, StringSplitOptions.None)[0];
                emp = emp.Replace("CN=", "");
                emp = emp.TrimEnd(',');
                emp = emp.Replace("\\, ", ", ");
                emp = emp.Split(',')[0];
                //emps.Add(emp);
            }
like image 745
James Wilson Avatar asked May 20 '13 19:05

James Wilson


People also ask

What is direct reports in Active Directory?

2.55 Attribute directReportsThis attribute contains the list of users that directly report to the user. The users that are listed as reports are those that have the property manager property set to this user. Each item in the list is a linked reference to the object that represents the user.

What is your direct report?

A direct report is an employee who reports to a manager, supervisor, or person in a leadership role within a company. People in charge of direct reports may also be referred to as direct reportees.


1 Answers

foreach (string objProperty in rs.Properties["DirectReports"])
            {
                isManager = true;
                string emp = objProperty.ToString();
                string[] setp = new string[1];
                setp[0] = "DC"; //If your users are in a OU use OU 

                emp = emp.Split(setp, StringSplitOptions.None)[0];
                emp = emp.Replace("CN=", "");
                emp = emp.TrimEnd(',');
                emp = emp.Replace("\\, ", ", ");
                emp = emp.Split(',')[0];
                //emps.Add(emp);
            }
like image 140
James Wilson Avatar answered Sep 30 '22 16:09

James Wilson