.NET Framework Class Library
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)
Syntax

Visual Basic (Declaration)
Public Function GetMembers ( _
    recursive As Boolean _
) As PrincipalSearchResult(Of Principal)
Visual Basic (Usage)
Dim instance As GroupPrincipal
Dim recursive As Boolean
Dim returnValue As PrincipalSearchResult(Of Principal)

returnValue = instance.GetMembers(recursive)
C#
public PrincipalSearchResult<Principal> GetMembers(
    bool recursive
)
Visual C++
public:
PrincipalSearchResult<Principal^>^ GetMembers(
    bool recursive
)
JScript
public function GetMembers(
    recursive : boolean
) : PrincipalSearchResult<Principal>

Parameters

recursive
Type: System..::.Boolean
A Boolean value that specifies whether the group is searched recursively.

Return Value

Type: System.DirectoryServices.AccountManagement..::.PrincipalSearchResult<(Of <(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.
Remarks

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.

Examples

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(); 
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5
See Also

Reference

Other Resources

Tags :


Page view tracker