I have added a AD group to a SharePoint group. Now I want to show the users which are member of the AD group (not name of the AD group) in a Site User web part. What is the best way to do it? because what ever I do the web part shows the name of the AD group not the members.
1 Answer
One way I know and have done is by using DirectoryServices.
Example:
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "ServerName", "UserName", "Password");
GroupPrincipal myGroup = GroupPrincipal.FindByIdentity(ctx, "GroupName");
if (myGroup != null)
{
PrincipalSearchResult<Principal> allMembers = myGroup.GetMembers(true);
foreach (Principal principal in allMembers)
{
string fullName = principal.Name;
string loginName = principal.SamAccountName;
string userPrincipalName = principal.UserPrincipalName;
//.....other user properties
}
}
-
Thank you. Is there a way to syncronize a Ad group with a SP group without using code or any user's credentials?– MedesCommented Dec 3, 2013 at 12:51
-
AFAIK its not possible. Years ago I had to develop a tool to synchronize AD groups with SP groups manually. Such a tool can be executed with a scheduled task or you can create a timer job that would run that logic. But my reasons were bigger then just showing the names in a webpart. Using AD groups was not an option for me and I had to use SP groups and synch them periodically. Commented Dec 3, 2013 at 13:21