public void ResetInheritance(PermissionsGetQuery options) { var spwebUrl = EnsureUrl(options.Url, options.ListId); try { using (var clientContext = new SPContext(spwebUrl, credentials.Get(spwebUrl))) { List splist = clientContext.Web.Lists.GetById(options.ListId); var splistItemCollection = splist.GetItems(options.Id.HasValue ? CAMLQueryBuilder.GetItem(options.Id.Value, new string[] { }) : CAMLQueryBuilder.GetItem(options.ContentId, new string[] { })); var lazyListItems = clientContext.LoadQuery(splistItemCollection.Include(item => item.HasUniqueRoleAssignments, item => item.Id)); clientContext.ExecuteQuery(); var splistItem = lazyListItems.First(); splistItem.ResetRoleInheritance(); clientContext.ExecuteQuery(); } } catch (Exception ex) { string itemId = options.Id.HasValue ? options.Id.Value.ToString(CultureInfo.InvariantCulture) : options.ContentId.ToString(); string message = string.Format("An exception of type {0} occurred in the SPPermissionsService.ResetInheritance() method for ListId: {1} ItemId: {2}. The exception message is: {3}", ex.GetType(), options.ListId, itemId, ex.Message); SPLog.RoleOperationUnavailable(ex, message); throw new SPInternalException(message, ex); } }
public SPPermissions Get(int userOrGroupId, PermissionsGetQuery options) { var spwebUrl = EnsureUrl(options.Url, options.ListId); try { using (var clientContext = new SPContext(spwebUrl, credentials.Get(spwebUrl))) { List splist = clientContext.Web.Lists.GetById(options.ListId); var splistItemCollection = splist.GetItems(options.Id.HasValue ? CAMLQueryBuilder.GetItem(options.Id.Value, new string[] { }) : CAMLQueryBuilder.GetItem(options.ContentId, new string[] { })); var listItemRoleAssignments = clientContext.LoadQuery( splistItemCollection.Select(item => item.RoleAssignments.GetByPrincipalId(userOrGroupId)).Include( roleAssignment => roleAssignment.Member, roleAssignment => roleAssignment.RoleDefinitionBindings.Include( roleDef => roleDef.Id, roleDef => roleDef.Name, roleDef => roleDef.Description))); clientContext.ExecuteQuery(); return(listItemRoleAssignments.First().ToPermission()); } } catch (Exception ex) { string itemId = options.Id.HasValue ? options.Id.Value.ToString(CultureInfo.InvariantCulture) : options.ContentId.ToString(); string message = string.Format("An exception of type {0} occurred in the SPPermissionsService.Get() method for a User or Group with Id: {1} ListId: {2} ItemId: {3}. The exception message is: {4}", ex.GetType(), userOrGroupId, options.ListId, itemId, ex.Message); SPLog.RoleOperationUnavailable(ex, message); throw new SPInternalException(message, ex); } }
public void Remove(int[] userOrGroupIds, PermissionsGetQuery options) { if (userOrGroupIds != null && userOrGroupIds.Length > 0) { var spwebUrl = EnsureUrl(options.Url, options.ListId); try { using (var clientContext = new SPContext(spwebUrl, credentials.Get(spwebUrl))) { List splist = clientContext.Web.Lists.GetById(options.ListId); var splistItemCollection = splist.GetItems(options.Id.HasValue ? CAMLQueryBuilder.GetItem(options.Id.Value, new string[] { }) : CAMLQueryBuilder.GetItem(options.ContentId, new string[] { })); var lazyListItems = clientContext.LoadQuery(splistItemCollection.Include(item => item.HasUniqueRoleAssignments, item => item.Id)); clientContext.ExecuteQuery(); var splistItem = lazyListItems.First(); foreach (int userOrGroupId in userOrGroupIds) { splistItem.RoleAssignments.GetByPrincipalId(userOrGroupId).DeleteObject(); } clientContext.ExecuteQuery(); } } catch (Exception ex) { string listItemId = options.Id.HasValue ? options.Id.Value.ToString(CultureInfo.InvariantCulture) : options.ContentId.ToString(); string userOrGroupIdsString = string.Join(", ", userOrGroupIds); string message = string.Format("An exception of type {0} occurred in the SPPermissionsService.Remove() method for Users or Groups with Ids: {1} ListId: {2} ItemId: {3}. The exception message is: {4}", ex.GetType(), userOrGroupIdsString, options.ListId, listItemId, ex.Message); SPLog.RoleOperationUnavailable(ex, message); throw new SPInternalException(message, ex); } } }