The below is a sample of retrieving a Manager / ReportsTo DirectoryEntry from Active Directory for a specified account.
1: using System.DirectoryServices;
2: using System.DirectoryServices.ActiveDirectory;
1: private DirectoryEntry GetDirectoryObject()
2: {
3: DirectoryEntry de = new DirectoryEntry(_path, _username, _password, AuthenticationTypes.Secure);
4: return de;
5: }
6:
7: private DirectoryEntry GetUser(string account)
8: {
9: try
10: {
11: DirectoryEntry de = GetDirectoryObject();
12: DirectorySearcher des = new DirectorySearcher();
13: des.SearchRoot = de;
14:
15: des.Filter = "(&(objectClass=user)(SAMAccountName=" + account + "))";
16: des.SearchScope = SearchScope.Subtree;
17: SearchResult results = des.FindOne();
18:
19: if (!(results == null))
20: {
21: de = new DirectoryEntry(results.Path, _username, _password, AuthenticationTypes.Secure);
22: return de;
23: }
24: else
25: {
26: return null;
27: }
28: }
29: catch (DirectoryServicesCOMException dsce)
30: {
31: throw;
32: }
33: }
34:
35: private DirectoryEntry GetManager(string account)
36: {
37: try
38: {
39: DirectoryEntry de = GetUser(account);
40: return demanager = new DirectoryEntry(_path + "/" + de.Properties["manager"].Value.ToString(), _username, _password);
41: }
42: catch (DirectoryServicesCOMException dsce)
43: {
44: throw;
45: }
46: }
47:
48: private DirectoryEntry GetManager(DirectoryEntry de)
49: {
50: try
51: {
52: return demanager = new DirectoryEntry(_path + "/" + de.Properties["manager"].Value.ToString(), _username, _password);
53: }
54: catch (DirectoryServicesCOMException dsce)
55: {
56: throw;
57: }
58: }