0

Hello everybody I am quite new to the Sharepoint, I am trying to get all the AD groups who have permissions on a sitecollection. I am able to get all the SPgroups and users using the code below in my simple C# console application:

SPRoleAssignmentCollection roleass = web.RoleAssignments;

foreach (SPRoleAssignment myRole in roleass)
{
    if (myRole.Member is SPGroup)
    {
        SPGroup group = web.Groups.GetByName(myRole.Member.Name);
        foreach (SPUser user in group.Users)
        {
            Console.WriteLine(user.Name);
        }
    }
}

how can I get the AD groups instead?

0

First you have to add "System.DirectoryServices.AccountManagement" reference to your project in order to deal with AD groups.

You can Refer below code to send email to SharePoint and AD group users. However you can get the different properties of AD users from below code based on upon your requirement.

 string toEmail = string.Empty;
SPRoleAssignmentCollection roleass = web.RoleAssignments;

foreach (SPRoleAssignment myRole in roleass)
{
    if (myRole.Member is SPGroup)
    {
        SPGroup group = web.Groups.GetByName(myRole.Member.Name);
foreach (SPUser user in group.Users)
        {
         bool domainGroup = user.IsDomainGroup;
         #region CodeForADGroup
         if (domainGroup)
            {
             //Get AD Group users to send email
             toEmail += GetADUsersEmail(user);
            }
         #endregion
         }
        else
           {
            toEmail += user.Email + ",";
           }
    }
}

public static string GetADUsersEmail(SPUser ADGroup)
        {
            string ADEmailAddress = string.Empty;
            try
            {
                var groupName = ADGroup.Name;
                var principalContext = new PrincipalContext(ContextType.Domain, DomainName);
                GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName);

                if (groupPrincipal != null)
                {
                    var members = groupPrincipal.GetMembers(true);
                    var membersList = new ArrayList();
                    foreach (Principal member in members)
                    {
                        membersList.Add(member.DisplayName);
                        var ADUser = UserPrincipal.FindByIdentity(principalContext, member.SamAccountName);
                        ADEmailAddress += ADUser.EmailAddress + ",";
                    };
                }

            }
            catch (Exception)
            {
                throw;
            }
            return ADEmailAddress;
        }
  • thanks, I am not able to get the users from different domains I am using SPPrincipal instead, How to get users from different domains any idea? – Meerza Khizar Bayeg Oct 21 '18 at 19:52
  • @MeerzaKhizarBayeg I don't know if it is possible to get users from multiple domains from AD. However, if you know the domain name, a bad way to achieve this would be to call the same method with different domain name. Please upvote and mark the answer as accepted since it helped you. – Sohail Shaikh Oct 22 '18 at 12:08
  • Sohail Sheikh: Please see this thread: sharepoint.stackexchange.com/questions/251272/… – Meerza Khizar Bayeg Oct 22 '18 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.