Link to home
Start Free TrialLog in
Avatar of GiftsonDJohn
GiftsonDJohnFlag for India

asked on

C# Get Active Directory Members from a Distribution Group

Hi,

I am trying to retrieve all the members from Active Directory using C# AD services.

I am able to retrieve all the members from the top level group. In our organisation they have defined some of Group Names start with * symbol. I am not able to locate the AD Group directly using the distinguishedName property.

This problem I am facing only if such group is added into some other group. for ex.

Group1 //(Main Group) I am able to locate the group
     Member1 //(Members) I am able to retrieve the members
     Member2
     Member3
    Group11 //(Sub Group) I am able to locate the group.
    *Group12 //(Sub Group with * symbol) I am unable to locate the group.

I need some help on this.
DirectoryEntry gc;
DirectoryEntry searchRoot = null;
DirectorySearcher searcher;
SearchResultCollection result;
 
// Get the directoryentry of the Global Catalog root
gc = new DirectoryEntry("GC:");
 
foreach (DirectoryEntry child in gc.Children)
{
          searchRoot = child;
}
 
searcher = new DirectorySearcher(searchRoot, string.Format("(&(|(objectCategory=person)(objectCategory=group))(sAMAccountName={0}))", searchName), new string[] { "distinguishedName", "mail", "sAMAccountName" }, SearchScope.Subtree);
                
SearchResult searchResult = searcher.FindOne();
 
if (searchResult != null)
{
        DirectoryEntry entry = new DirectoryEntry(searchResult.Path);
 
         if (IsGroup(GetValue(entry, "objectCategory")))
         {
                 IterateGroup(entry, searchRoot);
         }
         else
         {
               textBox2.AppendText(GetValue(entry, "GivenName") + " " + GetValue(entry, "SN"));
               textBox2.AppendText(Environment.NewLine);
         }
}
else
         textBox2.AppendText("User/Group not found in Active Directory");
 
 
private void IterateGroup(DirectoryEntry groupentry, DirectoryEntry searchRoot)
        {
            foreach (object member in groupentry.Properties["member"])
            {
                DirectorySearcher searcher = new DirectorySearcher(searchRoot, string.Format("(&(|(objectCategory=person)(objectCategory=group))(distinguishedName={0}))", member), new string[] { "distinguishedName", "mail", "sAMAccountName" }, SearchScope.Subtree);
 
                SearchResult searchResult = searcher.FindOne();
 
                if (searchResult != null)
                {
                    DirectoryEntry entry = searchResult.GetDirectoryEntry();
 
                    if (IsGroup(GetValue(entry, "objectCategory")))
                    {
                        IterateGroup(entry, searchRoot);
                    }
                    else
                    {
                        textBox2.AppendText(GetValue(entry, "GivenName") + " " + GetValue(entry, "SN"));
                        textBox2.AppendText(Environment.NewLine);
                    }
                }
            }
        }

Open in new window

Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

This is perhaps not the answer you want, but it can be a usefull resource for you.
Howto: (Almost) Everything In Active Directory via C#
http://www.codeproject.com/KB/system/everythingInAD.aspx
Avatar of GiftsonDJohn

ASKER

Hi Dhaest,

Thanks for your reply. I know about it. I am able to retrieve all the members of the group. I am not able to get only when the name of the group starts like *GroupName. I need assistance to overcome this.
ASKER CERTIFIED SOLUTION
Avatar of GiftsonDJohn
GiftsonDJohn
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial