Enumerating Members of a Group

This topic includes code examples for enumerating the members of a group. If the group has many members, you can get a result set by calling the IADsGroup::Members method. For more information about the ADSI IADsGroup::Members method, see "IADsGroup::Members" in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.

If you expect that the group could have more than 1000 members in the future, you must use ranged retrieval as shown in Enumerating Members in a Large Group to enumerate all of the group members.

The following code example shows how to get members using the SearchResult property Properties.

DirectoryEntry group = new DirectoryEntry("LDAP://CN=Sales,DC=Fabrikam,DC=COM");
foreach(object dn in group.Properties["member"] )
{
    Console.WriteLine(dn);
}

The following code example shows how to get members using the Invoke method to call the ADSI IADsGroup::Members method.

DirectoryEntry group = new DirectoryEntry("LDAP://CN=Sales,DC=Fabrikam,DC=COM");
object members = group.Invoke("Members", null);
foreach(object member in (IEnumerable)members)
{
    DirectoryEntry x = new DirectoryEntry(member);
    Console.WriteLine(x.Name);
}

Use the previous code to retrieve any standard properties of the DirectoryEntry object. To retrieve properties that are specific to a particular Active Directory Domain Services schema class such as User, use the Properties collection of the DirectoryEntry object. The MSDN technical article, Modifying Object Properties in an Active Directory Hierarchy, provides an example of how to access and modify members of the Properties collection.

To learn about properties that are available for a particular Active Directory Domain Services schema class, see the Active Directory Schema reference in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.

See Also

Reference

DirectoryEntry
SearchResult
System.DirectoryServices

Concepts

Group Management

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.