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