I am trying to use GroupPrincipal
(part of the System.DirectoryServices.AccountManagement
namespace) to populate a list of type string, so I can ch
This modification of your code works (I made tests to ensure):
using System.DirectoryServices.AccountManagement;
private static readonly string DomainName = "domaincontrollercomputer.domain.com";
private static readonly string DomainContainer = "DC=DOMAIN,DC=COM";
private static readonly string ADGroupName = "AD Group Name";
private List<string> GroupName {get;set;}
private void populateGroups()
{
using (var ctx = new PrincipalContext(ContextType.Domain, DomainName, DomainContainer))
{
using (var grp = GroupPrincipal.FindByIdentity(ctx, ADGroupName))
{
GroupName = new List<string>();
foreach (var member in grp.GetMembers())
{
GroupName.Add(member.SamAccountName);
}
}
}
}
I think you have a simple typo in your method - you're getting the group principal into SearchGroup
(check for NULL, btw!!) and then you're grabbing the members off GroupName
??
Try this:
private void populateGroups()
{
GroupPrincipal SearchGroup = GroupPrincipal.FindByIdentity(context, "Group Name");
if(SearchGroup != null)
{
GroupName = new List<string>();
// call 'GetMembers' on 'SearchGroup' here!!
foreach (UserPrincipal p in SearchGroup.GetMembers())
{
GroupName.add(p.SamAccountName);
}
}
}