GroupPrincipal.GetMembers Method

Definition

Returns a collection of the principal objects that is contained in the group.

Overloads

GetMembers()

Returns a collection of the principal objects that is contained in the group.

GetMembers(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.

GetMembers()

Source:
Group.cs
Source:
Group.cs
Source:
Group.cs

Returns a collection of the principal objects that is contained in the group.

public:
 System::DirectoryServices::AccountManagement::PrincipalSearchResult<System::DirectoryServices::AccountManagement::Principal ^> ^ GetMembers();
public System.DirectoryServices.AccountManagement.PrincipalSearchResult<System.DirectoryServices.AccountManagement.Principal> GetMembers ();
member this.GetMembers : unit -> System.DirectoryServices.AccountManagement.PrincipalSearchResult<System.DirectoryServices.AccountManagement.Principal>
Public Function GetMembers () As PrincipalSearchResult(Of Principal)

Returns

A PrincipalSearchResult<T> object that contains the principal objects that are members of the group, or an empty collection if the group has no members.

Remarks

This method does not search the current group recursively. Therefore, group objects may be returned in the principal object collection.

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.

Applies to

GetMembers(Boolean)

Source:
Group.cs
Source:
Group.cs
Source:
Group.cs

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.

public:
 System::DirectoryServices::AccountManagement::PrincipalSearchResult<System::DirectoryServices::AccountManagement::Principal ^> ^ GetMembers(bool recursive);
public System.DirectoryServices.AccountManagement.PrincipalSearchResult<System.DirectoryServices.AccountManagement.Principal> GetMembers (bool recursive);
member this.GetMembers : bool -> System.DirectoryServices.AccountManagement.PrincipalSearchResult<System.DirectoryServices.AccountManagement.Principal>
Public Function GetMembers (recursive As Boolean) As PrincipalSearchResult(Of Principal)

Parameters

recursive
Boolean

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

Returns

A PrincipalSearchResult<T> object that contains the principal objects that are members of the group, or an empty collection if the group has no members.

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

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.

Applies to