SharePoint: ResetRoleInheritance and BreakRoleInheritance wrappers
A while back I wrote about a number of methods lead to reinitializing of SPWeb-object and, as the result, to resetting AllowUnsafeUpdates to false (SharePoint: Updates are currently disallowed on GET requests). Methods ResetRoleInheritance and BreakRoleInheritance of SPListItem class are the examples of such behavior. I use them very often, that is why I decided to develop a couple of wrappers that allow to restore AllowUnsafeUpdates to true automatically after original methods have finished their work.
public static void SafeResetRoleInheritance(SPListItem item)
{
bool oldAllowUnsafeUpdate = item.Web.AllowUnsafeUpdates;
item.ResetRoleInheritance();
if(item.Web.AllowUnsafeUpdates != oldAllowUnsafeUpdate)
item.Web.AllowUnsafeUpdates = oldAllowUnsafeUpdate;
}
public static void SafeBreakRoleInheritance(SPListItem item, bool copyRoleAssignments)
{
bool oldAllowUnsafeUpdate = item.Web.AllowUnsafeUpdates;
item.BreakRoleInheritance(copyRoleAssignments);
if (item.Web.AllowUnsafeUpdates != oldAllowUnsafeUpdate)
item.Web.AllowUnsafeUpdates = oldAllowUnsafeUpdate;
}
Each wrapper preserves the current value of SPWeb.AllowUnsafeUpdates, invokes the original method, then tests whether the AllowUnsafeUpdates is changed and if so, it restores the old value back.