Query Active directory to get the email property of a distinguished name directly?

19,089

There is no "magic" shortcut to getting the e-mail of a manager.

Once you've retrieved the DN (distinguished name) of your manager (in a string variable called managerDN), you need to again bind to Active Directory by creating another instance of a DirectoryEntry to grab the manager's user info.

Try something like this:

 .....(your other code up here)......
 else
 {
     string managerDN = result.Properties["manager"][0].ToString();

     // fully-qualified DN for manager
     string managerFQDN = "LDAP://" + managerDN;

     DirectoryEntry manager = new DirectoryEntry(managerFQDN);

     if(manager != null)
     {
        // get e-mail of manager
        if(manager.Properties["mail"] != null && 
           manager.Properties["mail"].Count > 0)
        {
           string managersEMail = manager.Properties["mail"].Value.ToString();
           return managersEMail;
        }
     }

     // we couldn't retrieve the manager's e-mail  
     return string.Empty;
}
Share:
19,089

Related videos on Youtube

Splunk
Author by

Splunk

Updated on September 15, 2022

Comments

  • Splunk
    Splunk over 1 year

    I am doing some querying in active directory at the moment, our database user id matches that of the active directory user id.

    I am passing the user id along with the domain and the path to get what I need. My endeavour is to get the email address of the manager from the passed user id. What I am returning when I get the manager property is the distinguished name.

    Finding a user's manager record in Active Directory

    This above post is my exact problem, but it's an old post and there are no further descriptives on how to move forward and the OP knew what to do next with the distinguished name. Truth is, I don't.

    So my question is, how to I get the email address property from the distinguished name which I have thus far stored as a string with a prefix of LDAP:// + "MyDistinguishedName"?

     public string GetManagerEmail(string ActiveDirectoryPath, string ActiveDirectoryDomain, bool email)
        {
    
            DirectoryEntry entry = new DirectoryEntry(ActiveDirectoryPath);
    
            try
            {
                DirectorySearcher search = new DirectorySearcher(entry);
    
                search.Filter = "(SAMAccountName=" + workerID + ")";
                search.PropertiesToLoad.Add("cn");
                search.PropertiesToLoad.Add("givenname");  //firstname
                search.PropertiesToLoad.Add("sn");//surname
                search.PropertiesToLoad.Add("manager");
                search.PropertiesToLoad.Add("email");
                SearchResult result = search.FindOne();
    
                if (null == result)
                {
                    return workerID;
                }
                if (email)
                {
                    return (string)result.Properties["email"][0];
                }
                else
                {
                    return (string)result.Properties["manager"][0];
                    //return (string)result.Properties["manager"].IndexOf[];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error. " + ex.Message);
    
            }
            finally
            {
                entry.Close();
            }
        }
    

    Above is the method I use to get the data I need. Any input or improvements would be appreciated.

    Thanks

    THIS IS MY SOLUTION FOR THOSE THAT MAY BE INTERESTED

                string domainAndUsername = ActiveDirectoryDomain + @"\" + workerID;
            DirectoryEntry manager = new DirectoryEntry(ActiveDirectoryPath);
    
            try
            {
                if (manager != null)
                {
                    // get e-mail of manager 
                    if (manager.Properties["mail"] != null && manager.Properties["mail"].Count > 0)
                    {
                        string managersEMail = manager.Properties["mail"].Value.ToString();
                        return managersEMail;
                    }
                }
    
                //No email available, use contract manager
                return string.Empty;
    
            }
            catch (Exception ex)
            {
                throw new Exception("Error. " + ex.Message);
    
            }
            finally
            {
                manager.Close();
            }
    
  • Splunk
    Splunk over 11 years
    Thanks marc_s, string managersEMail has the value "System.DirectoryServices.PropertyValueCollection", any suggestion on what is missing?
  • Splunk
    Splunk over 11 years
    Cracked it manager.Properties["mail"].Value.ToString(); THanks for your help =1 and answer XD
  • HungryPipo
    HungryPipo over 8 years
    You can use if(manager.Properties.Contains("mail")) instead of if(manager.Properties["mail"] != null && manager.Properties["mail"].Count > 0)
  • HungryPipo
    HungryPipo over 8 years
    Also I highly recommend manager.RefreshCache(string[]{"mail"}); Otherwise it will be slow because all the AD properties will be loaded not just the "mail" property you're looking for
  • HungryPipo
    HungryPipo over 8 years
    Also wrap DirectorySearcher and DirectoryEntry in using statements