title: Getting manager email id from active directory
body: <p>How to get user manager email id from active directory? I have written code with which I can get user's firstname, lastname, email id and his manager name based on userid, but I want to get manager email id along with his manager name.</p>
  
  <p>Can somebody please help me how to get this? Here is my code:</p>
  
  <pre><code>protected void ddlAdsuser_SelectedIndexChanged(object sender, EventArgs e)
  {
      DirectoryEntry root = new DirectoryEntry("LDAP://RootDSE");
      string myDomain = root.Properties["defaultNamingContext"].Value.ToString();
      DirectoryEntry domain = new DirectoryEntry("LDAP://" + myDomain);
      DirectorySearcher dsUsers = new DirectorySearcher(domain);
      dsUsers.Filter = "(userPrincipalName=" + ddlAdsuser.Text + ")";
      foreach (SearchResult sResultSet in dsUsers.FindAll())
          {
              lblfname.Text = GetProperty(sResultSet, "givenName");
              lbllname.Text = GetProperty(sResultSet, "sn");
              lblemail.Text = GetProperty(sResultSet, "mail");
  
              string Manager = string.Empty;
              Manager = GetProperty(sResultSet, "manager");
              if (Manager != "")
              {
                  if (Manager.Contains("CN="))
                  {
                      int Length = Manager.IndexOf(',');
                      Manager = Manager.Substring(3, Length - 3);
                  }
                  else
                  {
                      Manager = string.Empty;
                  }
              }            
              lblManagerID.Text = Manager;  //Here displaying the manager name.
          }     
  }
  
  public static string GetProperty(SearchResult searchResult, string PropertyName)
  {
      if (searchResult.Properties.Contains(PropertyName))
      {
          return searchResult.Properties[PropertyName][0].ToString();
      }
      else
      {
          return string.Empty;
      }
  }
  </code></pre>