Getting a list of direct reports from the current user

I checked two other threads and even used code from one, but it never fills the list. When I open Active Directory users and computers and go to my manager within the organization, I see his list of direct reports.

What I'm trying to do is access this list through code. So far I have not found anything. It 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 tried using Reports and directReports and none of them work with error.

This method loads the registered user or any user that I pass. I can access all of my properties, but I cannot access their direct reports.

What am I missing?

Found 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); } 
+4
source share
1 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); } 
+4
source

Source: https://habr.com/ru/post/1481819/


All Articles