locked
Get Email addresses of a group RRS feed

  • Question

  • User-846088618 posted

    Hello everybody,

    I have a huge amount of time for searching this but could not find it that how i can get email address of users whom belongs to a ABC group. however i am able to find email address of a specific user.

    Pleas help ... My code is as below

    Dim dEntry As New DirectoryEntry("LDAP://Server")

    Dim dSearch As New DirectorySearcher(dEntry, "(objectclass=ABC)")

    dSearch.PropertiesToLoad.Add("mail")

    dSearch.PropertiesToLoad.Add("sAMAccountName")

    Dim colResult As SearchResultCollection = dSearch.FindAll  ' ** it is returning 0 elements

     

    RS

    Tuesday, November 18, 2008 12:42 AM

Answers

  • User-845805365 posted
    Hi please try this piece of code, thsi is working for me...

       

    public static DirectoryEntry GetDirectoryEntry(string DomainReference)
    {
    string ADFullPath = "LDAP://yourDomainName";
    DirectoryEntry de = new DirectoryEntry(ADFullPath + DomainReference, YourUserName, YourPassword, AuthenticationTypes.Secure);
    return de;
    }

    private static string GetLDAPDomain()
    {
    StringBuilder LDAPDomain = new StringBuilder();
    string[] LDAPDC = domainName
    ".Split('.');
    for (int i = 0; i < LDAPDC.GetUpperBound(0) + 1; i++)
    {
    LDAPDomain.Append("
    DC=" + LDAPDC[i]);
    if (i < LDAPDC.GetUpperBound(0))
    {
    LDAPDomain.Append("
    ,");
    }
    }
    return LDAPDomain.ToString();
    }
     
    protected void Page_Load(object sender, EventArgs e)
    {
    string[] email = new string[0];
    DirectoryEntry entry = GetDirectoryEntry("/" + GetLDAPDomain());
    string groupName = "Your Groupd Name to CHeck"; //Group NAme

    DirectorySearcher groupSearch = new DirectorySearcher(entry);
    groupSearch.Filter = "(SAMAccountName=" + groupName + ")";
    groupSearch.PropertiesToLoad.Add("member");
    SearchResult groupResult = groupSearch.FindOne(); // getting the members who belongs to the concern groupname
    if (groupResult != null)
    {

    email = new string[groupResult.Properties["member"].Count]; //creatign an array to store all the email address
    for (int iSearchLoop = 0; iSearchLoop < groupResult.Properties["member"].Count; iSearchLoop++)
    {
    string userName = groupResult.Properties["member"][iSearchLoop].ToString();
    int index = userName.IndexOf(',');
    userName = userName.Substring(0, index).Replace("CN=", "").ToString(); // the name of the user will be fetched.

    DirectorySearcher search = new DirectorySearcher(entry);
    search.Filter = "(name=" + userName + ")";
    search.PropertiesToLoad.Add("mail");
    SearchResult result = search.FindOne(); //finding the mail id
    if (result != null)
    {
    email[iSearchLoop] = result.Properties["mail"][0].ToString(); //assigning the mail i d to an array....
    }
    }
    		//email finally at the end of the loop you will get all the email id's who belong to the group

    }

    }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 1, 2008 6:27 AM

All replies

  • User-2009597737 posted
    Your code looks interesting. You are probably missing some steps: Bind to AD Dim dEntry As New DirectoryEntry("LDAP://Server") -- should be ok Set the filter to group filter="(&(&(objectClass=group)(SAMAccountName=" + ABC + ")))"; // you are searching for groups and the group name is ABC Create a result object and loop thru the result set. The c# equvalient will be: System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry); mySearcher.Filter = filter; mySearcher.SearchScope = SearchScope.Subtree; //mySearcher.PropertiesToLoad.Add("member"); SearchResult mySearchResult = mySearcher.FindAll(); Loop thru this result set. You will get members of the group ABC. You need to create a directory entry for each member and get the member's e-mail. I do not know of a simpler/easier way. I had to do the same: expand an AD group and get the e-mails of each member. I used c#. Will be curious to know if there is a easier way to get this.
    Wednesday, November 19, 2008 1:10 PM
  • User-846088618 posted

    Dear Raghu Thank you so much for replying on my post, i was desperately searhing the answer of it. I'll give a try to your logic.

    I am stuck on 1 more thing in Active Directory and unable to find any help on Internet. I need to set a flag for any user in ABC group to enable/disable email address. so when i access UserA then i could know his email flag is enable or disable. (it could be any boolean value for a user which can be set as true or false)

     Could you please post the code .

     your efforts are greatly appreciated.

     

    RS

    Friday, November 21, 2008 2:26 AM
  • User-845805365 posted
    Hi please try this piece of code, thsi is working for me...

       

    public static DirectoryEntry GetDirectoryEntry(string DomainReference)
    {
    string ADFullPath = "LDAP://yourDomainName";
    DirectoryEntry de = new DirectoryEntry(ADFullPath + DomainReference, YourUserName, YourPassword, AuthenticationTypes.Secure);
    return de;
    }

    private static string GetLDAPDomain()
    {
    StringBuilder LDAPDomain = new StringBuilder();
    string[] LDAPDC = domainName
    ".Split('.');
    for (int i = 0; i < LDAPDC.GetUpperBound(0) + 1; i++)
    {
    LDAPDomain.Append("
    DC=" + LDAPDC[i]);
    if (i < LDAPDC.GetUpperBound(0))
    {
    LDAPDomain.Append("
    ,");
    }
    }
    return LDAPDomain.ToString();
    }
     
    protected void Page_Load(object sender, EventArgs e)
    {
    string[] email = new string[0];
    DirectoryEntry entry = GetDirectoryEntry("/" + GetLDAPDomain());
    string groupName = "Your Groupd Name to CHeck"; //Group NAme

    DirectorySearcher groupSearch = new DirectorySearcher(entry);
    groupSearch.Filter = "(SAMAccountName=" + groupName + ")";
    groupSearch.PropertiesToLoad.Add("member");
    SearchResult groupResult = groupSearch.FindOne(); // getting the members who belongs to the concern groupname
    if (groupResult != null)
    {

    email = new string[groupResult.Properties["member"].Count]; //creatign an array to store all the email address
    for (int iSearchLoop = 0; iSearchLoop < groupResult.Properties["member"].Count; iSearchLoop++)
    {
    string userName = groupResult.Properties["member"][iSearchLoop].ToString();
    int index = userName.IndexOf(',');
    userName = userName.Substring(0, index).Replace("CN=", "").ToString(); // the name of the user will be fetched.

    DirectorySearcher search = new DirectorySearcher(entry);
    search.Filter = "(name=" + userName + ")";
    search.PropertiesToLoad.Add("mail");
    SearchResult result = search.FindOne(); //finding the mail id
    if (result != null)
    {
    email[iSearchLoop] = result.Properties["mail"][0].ToString(); //assigning the mail i d to an array....
    }
    }
    		//email finally at the end of the loop you will get all the email id's who belong to the group

    }

    }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 1, 2008 6:27 AM
  • User-846088618 posted

    Thank you so much Nimesh for sharing the code.

     I'll try it tomarrow

    Saturday, December 6, 2008 5:19 AM
  • User-846088618 posted

    It works like a treat, Could you tell me which book or web site you followed.

     

    Thanks in Advance

    Sunday, December 7, 2008 5:09 AM
  • User-165682064 posted

    Nimesh...your solution was really good....Thanks! for sharing... 

    Tuesday, February 16, 2010 5:04 AM