Export (0) Print
Expand All

GroupPrincipal.GetMembers Method (Boolean)

Returns a collection of the principal objects that is contained in the group. When the recursive flag is set to true, this method searches the current group recursively and returns all nested group members.

Namespace:  System.DirectoryServices.AccountManagement
Assembly:  System.DirectoryServices.AccountManagement (in System.DirectoryServices.AccountManagement.dll)

public PrincipalSearchResult<Principal> GetMembers(
	bool recursive
)

Parameters

recursive
Type: System.Boolean

A Boolean value that specifies whether the group is searched recursively.

Return Value

Type: System.DirectoryServices.AccountManagement.PrincipalSearchResult<Principal>
A [T:System.DirectoryServices.AccountManagement.PrincipalSearchResult'1] object that contains the principal objects that are members of the group, or an empty collection if the group has no members.

The returned principal collection does not contain group objects when the recursive flag is set to true; only leaf nodes are returned. For example, when a group that contains a computer object and a group object (with only user principals) is searched recursively, the returned collection contains the computer object and the user principal objects in the nested group. Since the group object is not a leaf, even when it is empty, it is not returned in the recursive search. When the recursive flag is set to false, the returned collection may contain group objects.

Members are returned without respect to the context. For example, if an AD DS context based at "CN=SpecialUsers,DC=Fabrikam,DC=com", the PrincipalFindResult set will include group members that are located under "CN=NormalUsers,DC=Fabrikam,DC=com" also, even though they fall under a scope that is not part of the context that is searched. The returned principal collection may also contain members that are located in a different store than the group.

The following code connects to the LDAP domain "fabrikam.com" with the username set to "administrator" and the password set to "SecretPwd123" in the PrincipalContext constructor.

A search is performed to find the group that has the name "Domain Admins" under the container specified in the PrincipalContext constructor "DC=fabrikam,DC=com." If the group is found, all the principals that are members of this group, which includes recursive members, are then enumerated

PrincipalContext ctx = new PrincipalContext(ContextType.Domain,                                                                    
                                            "fabrikam.com", 
                                            "DC=fabrikam,DC=com", 
                                            "administrator", 
                                            "SecretPwd123");
        
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, 
                                                   IdentityType.Name, 
                                                   "Domain Admins");

if (grp != null)
{
    foreach (Principal p in grp.GetMembers(true))
    {
         Console.WriteLine(p.Name);
    }
    grp.Dispose();
}

ctx.Dispose(); 

.NET Framework

Supported in: 4.6, 4, 3.5

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft