Hi,
I have a custom list which inherits the permission from the site. When a list item is added, I would like to remove the permissions and provide permission to the user who created the request. There are more than 1000 users who have access to the site, and which is inherited by the list item.
I have used this code, but it takes a longer time to get executed. Is the below method correct? Is there any other approach which should be used.
public static void RemoveAllPermissions(SPListItem CurrentlistItem) { CurrentlistItem.BreakRoleInheritance(true); SPRoleAssignmentCollection SPRoleAssColn = CurrentlistItem.RoleAssignments; while (CurrentlistItem.RoleAssignments.Count > 0) { CurrentlistItem.RoleAssignments.Remove(0); ReloadListItem(CurrentlistItem); using (DisabledItemEventsScope scope = new DisabledItemEventsScope()) { CurrentlistItem.Update(); } } } public static SPListItem ReloadListItem(SPListItem item) { if (item == null) return null; return item.ParentList.GetItemByUniqueId(item.UniqueId); } public class DisabledItemEventsScope : SPItemEventReceiver, IDisposable { bool oldValue; public DisabledItemEventsScope() { this.oldValue = base.EventFiringEnabled; base.EventFiringEnabled = false; } public void Dispose() { base.EventFiringEnabled = oldValue; } }
How to optimize the code? Is the above code correct?
Thanks