Search code examples
c#active-directorydirectoryservices

Iterating the members of a large group in active directory


I need to iterate the members of a group using the System.DirectoryServices classes.

The problem I am seeing is that after I obtain the DirectoryEntry for the group, the "members" property only contains 1500 entries. In reality, the group has more than 4000.

Is there some setting that tells the DirectoryServices classes to not retrieve more than 1500 group members?


Solution

  • If you can, try using the System.DirectoryServices.AccountManagement namespace from .NET 3.5. I don't have any group quite that large to try - but you should be able to get those members:

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    GroupPrincipal group = GroupPrincipal.FindByIdentity("name of the group");
    
    if(group != null) 
    { 
        foreach(Principal p in group.Members)
        {
           // do something
        }
    }
    

    You can read more about the S.DS.AM namespace and its new abilities at MSDN Magazine: Managing Directory Security Principals in the .NET Framework 3.5