Created
April 23, 2021 08:35
-
-
Save chocolateRaavan/513c884c9996ce48e9ea5a73bc2fcacc to your computer and use it in GitHub Desktop.
Get User Groups from a SharePoint SubSite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class WebGroups | |
| { | |
| public static ClientContext _context = ClientContextFactory.GetContext(); | |
| public static void Main(string[] args) | |
| { | |
| var groups = GetWebGroups(); | |
| Console.WriteLine(groups); | |
| } | |
| public static IEnumerable<string> GetWebGroups() | |
| { | |
| var web = _context.Web; | |
| var roleAssignmentCollection = web.RoleAssignments; | |
| /* Member contains the Group Info, if a group is given access, or the User Info, if a user is given access directly | |
| * Role Definition Bindings give us the information related to levels of access a member has */ | |
| _context.Load(roleAssignmentCollection, roleAssignment => (roleAssignment.Include(r => r.Member, r => r.RoleDefinitionBindings))); | |
| _context.ExecuteQuery(); | |
| var groupTitles = new List<string>(); | |
| foreach(var roleAssignment in roleAssignmentCollection) | |
| { | |
| // Ensure that current member is of Group Type | |
| if (roleAssignment.Member.PrincipalType == Microsoft.SharePoint.Client.Utilities.PrincipalType.SharePointGroup) | |
| { | |
| var roleDefinitionBindings = roleAssignment.RoleDefinitionBindings; | |
| // Validate if the group has ability to ViewListItems. This is the basic level of permissioning which allows user to | |
| // View items in lists, documents in document libraries, and Web discussion comments. | |
| // If you want to Read more about this, proceed to https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-server/ee536458(v=office.15) | |
| if (roleDefinitionBindings.Any(r => r.BasePermissions.Has(PermissionKind.ViewListItems))) | |
| { | |
| // Do anything you want to do here | |
| var groupId = roleAssignment.Member.Id; | |
| groupTitle.Add(roleAssignment.Member.Title); | |
| } | |
| } | |
| } | |
| return groupTitles; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment