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();

Override SharePoint SaveButton to function as Save As Draft button:

There was a requirement for an application to save a list item as draft and then submit the list item as final once all the required information is available. SharePoint save button can be overridden to start a custom workflow if required. Remember save as draft and save Buttons are different.



<%@ Register TagPrefix="WebControl" Assembly="XXXXXX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c2b3ca69d93d7c67" namespace="XXXXXX"%>

In the custom list form
<wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbltop" RightButtonSeparator=" " runat="server">
<Template_RightButtons>
<WebControl:SubmitButton ID="SubmitButton1" Text="Submit" WorkflowKey="Server Request" runat="server" />
<WebControl:SaveDraftButton ID="SaveDraftButton1" text="Save Draft" runat="server"/>
<SharePoint:GoBackButton runat="server"/>
</Template_RightButtons>
</wssuc:ToolBar>

Code for Save as Draft button:
namespace XXXXXXX
{
[SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class SaveDraftButton : SaveButton
{
protected override bool SaveItem()
{

bool setPermissions = ListItem.Name == null;
bool success = base.SaveItem();
if (setPermissions)
{
EventMethods.SetItemInitialPermissions(ListItem);
}

return success;
}

[SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
protected override bool OnBubbleEvent(object source, EventArgs e)
{
bool flag;
string redirectUrl;
base.OnBubbleEvent(source, e);
flag = this.SaveItem();
redirectUrl = base.RedirectUrl;
SPUtility.Redirect(redirectUrl, SPRedirectFlags.UseSource, this.Context);
return flag;
}
}
}