title: C# - Look up a users manager in active directory
body: <p>Started using the <code>System.DirectoryServices.AccountManagement</code> namespace, to perform the lookup on a user in active directory (AD). <strong>I also need the user's manager</strong>, but I seem to have hit a bump in the road using this namespace. Current code to get a person:</p>
  
  <pre class="lang-cs prettyprint-override"><code>class Person {
      // Fields
      public string GivenName = null;
      public string Surname = null;
      public string DistinguishedName = null;
      public string Email = null;
      public string MangerDistinguishedName = null;  // Unable to set this
  
      // Constructor
      public Person(string userName) {
          UserPrincipal user = null;
  
          try {
              user = GetUser(userName);
  
              if (user != null) {
                  this.GivenName = user.GivenName;
                  this.Surname = user.Surname;
                  this.DistinguishedName = user.DistinguishedName;
                  this.Email = user.EmailAddress;
                  this.MangerDistinguishedName = user.&lt;NO SUCH PROPERTY TO FIND A MANAGER'S DISTINGUISHED NAME&gt;
              }
              else {
                  throw new MissingPersonException("Person not found");
              }
          }
          catch (MissingPersonException ex) {
              MessageBox.Show(
                  ex.Message
                  , ex.reason
                  , MessageBoxButtons.OK
                  , MessageBoxIcon.Error
              );
          }
          catch (Exception ex) {
              MessageBox.Show(
                  ex.Message
                  , "Error: Possible connection failure, or permissions failure to search for the username provided."
                  , MessageBoxButtons.OK
                  , MessageBoxIcon.Error
              );
          }
          finally {
              user.Dispose();
          }
      }
  </code></pre>
  
  <p>Execute search for the person</p>
  
  <pre class="lang-cs prettyprint-override"><code>    private UserPrincipal GetUser(string userName) {
          PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
          UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName);
  
          return user;
      }
  </code></pre>
  
  <p><strong>What is another way to directly access the distinguished name of the manager of a particular user?</strong></p>
  
  <ul>
  <li>Possible partial answer <a href="https://stackoverflow.com/questions/9498908/looping-through-active-directory-to-get-managers-and-direct-reports">here</a> in VB, but I see nothing about referring to managers.</li>
  <li>Another possible partial one <a href="https://stackoverflow.com/questions/1546871/system-directoryservices-vs-system-directoryservices-accountmanagement">here</a>, again, nothing about managers.</li>
  </ul>