Show Hide Existing Ribbon Options / Commands…


Once I wanted to hide some ribbon options for a given permission set. Basically, in SharePoint 2010 the rich text content area allows to insert web part directly on it. I wanted to security trim so, only the users that has Contributor permission level or higher would be able to perform the web part insert.

While figuring out how to do this, I came across an option that, to create a new custom control to perform the show hide ribbon options.

1) Create a custom control (inheriting from System.Web.UI.Control)

2) Override the OnPreRender method

3) Retrieve the ribbon from the current viewing page.

SPRibbon ribbon = SPRibbon.GetCurrent(Page);


4) Do the necessary permission check

if (!SPContext.Current.Web.DoesUserHavePermissions(
SPBasePermissions.AddAndCustomizePages) && ribbon != null)
{
ribbon.TrimById(“Ribbon.EditingTools.CPInsert.WebParts”);
}

5) SPContext.Current.Web.DoesUserHavePermissions will take a permission level as a parameter and returns true if the current user has the requested permission level.

6) Ribbon.TrimById will accept the id of a ribbon command or a group, and will trim it out (hide)

7) All available ribbon commands are defined a XML file. You can find it in the “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\GLOBAL\XML\CMDUI.XML

Once you have completed the control place it in the masterpage so all your pages get the ribbon option filtering by default.

That’s it Smile  Happy Coding!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s