Thursday, February 9, 2012

BreakRoleInheritance in SharePoint:

If we want to unique permissions to SharePoint objects (Lists, document libraries, folders etc..) we use BreakRoleInheritance. It takes a Boolean parameter which is either true or false.
BreakRoleInheritance(true) - means copy all the permissions from the parent and then maintain its own unique permissions for all the future changes on that object.
BreakRoleInheritance(false) - means do not copy any permissions from the parent and maintain its own unique permissions for all the future changes on the object.
For some reason BreakRoleInheritance(false) was not working and was throwing an error. I am not the only person to complain about this, but the same with many developers on the Google. Below is the final code which worked for me.

//Code to fix BreakRoleInheritance(false) issues
//with BreakRoleInheritance(true) permissions are inherited and then removed one by one.
//It is responsible for locking Permissions table for long time and potentially resulting
//into the deadlock
oWeb.AllowUnsafeUpdates = true;
SPFolder oPermissionFolder = oList.RootFolder.SubFolders.Add(FolderName);
oPermissionFolder.Item.BreakRoleInheritance(false);
oFolderItem = oPermissionFolder.Item;
oFolderItem["Title"] = false.ToString() + ";1";

if(!oWeb.AllowUnsafeUpdates)
oWeb.AllowUnsafeUpdates = true;

int raCount = oFolderItem.RoleAssignments.Count;
for (int i = raCount - 1; i >= 0; i--)
{
oFolderItem.RoleAssignments.Remove(i);
}

//oFolderItem.Update();
oFolderItem.SystemUpdate();

No comments:

Post a Comment