Get Count of members in a AD Group using PrincipalSearcher

2.7k views Asked by At

Env : Visual Studio 2013, FrameWork 4.5, Telerik Controls, C#, WebForm application

Using : System.DirectoryServices and System.DirectoryServices.AccountManagement

I'm making a search tools so a user can search for a active directory group name in multiple forest/domain.

The search return a list of 1 or more group and I put that list in a RadGrid (Telerik). Each row of the grid is a AD Group. I would like to display an additional information that show the user how many(count?) members(users) there is in that group.

private List<AdGroup> GetListOfGroupAD(string domain, string name, string samAccountName)
    {
        try
        {
            GroupPrincipal qbeGroup;
            using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain))
            {
                qbeGroup = new GroupPrincipal(ctx);
                qbeGroup.Name = !string.IsNullOrEmpty(name) ? name : "*";
                qbeGroup.SamAccountName = !string.IsNullOrEmpty(samAccountName) ? samAccountName : "*";
                PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
                ((DirectorySearcher)srch.GetUnderlyingSearcher()).PageSize = 500;

                List<AdGroup> listeGroupe = srch.FindAll()
                                      .OrderBy(x => x.SamAccountName)
                                      .Select(x => new AdGroup()
                                      {
                                          SamAccountName = x.SamAccountName,
                                          Description = x.Description,
                                          Domain = domain,
                                          NbMember = 0 //Can i Get a count of members in group here ?????
                                      })
                                      .ToList();
                return listeGroupe;
            }
        }
        catch (ArgumentNullException ex)
        {
            writeToLog(ex.Message, 1);
            return null;
        }
        catch (Exception ex)
        {
            writeToLog(ex.Message, 1);
            return null;
        }
    }

public class AdGroup
    {
        public string SamAccountName { get; set; }
        public string Description { get; set; }
        public string Domain { get; set; }
        public int NbMember { get; set; }
    }

Thank you for the help

Richard

1

There are 1 answers

2
Chase On BEST ANSWER

One approach is to specify the type of the search result as GroupPrincipal using .OfType() after the call to FindAll(), and then you can get the members of each group as a collection using the Members collection property or the GetMembers() method, which has an optional boolean argument to specify if you need to search the group recursively for nested members. At that point, get the size of the collection.

List<AdGroup> listeGroupe = srch.FindAll()
    .OfType<GroupPrincipal>()
    .OrderBy(x => x.SamAccountName)
    .Select(x => new AdGroup()
    {
        SamAccountName = x.SamAccountName,
        Description = x.Description,
        Domain = domain,
        NbMember = x.Members.Count
    })
    .ToList();