When entering text into the Rich HTML Editor with SharePoint 2010, you get a rich ‘ribbon’ experience, with a whole swag of stuff you can do – aptly titled “Format Text”.
This works for the Content Editor WebPart – and HTML content fields, such as Site Column – or Content Type column – like on a Publishing Page for example.
The editor includes a bunch of ‘tab groups’ (font, paragraph, etc) – with individual actions in each – like Bold, Italic, Underline, and so forth.
To *ADD* items to this set, you can use Declarative XML to create a ‘Custom Action’ – see the following for more info on how to do THAT.
What if you want to *REMOVE* some actions – or groups ?
And – if you need to drop actions/groups for different page types – different Master Page and/or Page Layout ?
Well – here is some C# code using the SharePoint API that will allow you to do this.
The basic premise is :
- Create a Visual Studio project
- Open a Web UserControl project type
- Add C# code to hide tabs
- Include a reference to this user control in the Master Page
Create Visual Studio Project
The (awesome) new Visual Studio 2010 IDE has a bunch of SharePoint related project types – w00t ! More along the lines of the beloved WSPBuilder style – which I still use for SharePoint 2007 development.
- Open Visual Studio 2010 – and click New > Project
- Choose the SharePoint 2010 group
- Pick the top entry “Empty SharePoint project” – give it a name : SPR.Utilities
- Change the option button to “Deploy as a farm solution” when prompted – and click Finish.
Add a Web User Control
Now we need to add the code for the Ribbon Item Hider – we don’t actually need a ‘feature’ – just need something that will be added to the CONTROLTEMPLATES folder – located inside the 14 Hive – otherwise known as the…. (ahem) … SharePointRoot folder.
- Right-click on the root project node (in bold) in the Solution Explorer
- Choose Add > SharePoint Mapped Folder
- When the Folder picker is shown, expand the TEMPLATE folder
- Click on CONTROLTEMPLATES – and then OK.
This adds a location that we can add our project code – and the ASCX (web user control).
We still need a sub-folder – otherwise the code pieces will land in the same folder as the SharePoint OOTB items – over 90 items in that folder ! *eek*
- Right-click on the CONTROLTEMPLATES folder – and click Add > Folder
- Change the name of the folder to be SPR.Utilities
Add the web user control
- Right-click on the SPR.Utilities folder
- Click Add > New Item
- Choose the User Control item, from the SharePoint 2010 group
- Rename it to be RibbonItemHider
Add the code – yay !
This UserControl is to live on a SharePoint Master Page, and thus there will always be a SPContext, and an underlying SPRibbon object available.
We really just need to do the following logic :
- Grab a reference to the SPRibbon object on the page
- Trim out the items we DON’T want to show
- And – um, that’s it !!
Have to add a reference to the following DLL – can paste this path into the dialog shown when you choose “Add Reference”.
C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14ISAPIMicrosoft.Web.CommandUI.dll
And now the code :
Within the Page_Load event, just add the following code
SPRibbon ribbon = SPRibbon.GetCurrent(this.Page);
if (ribbon != null)
You’ll get some squiggly lines underneath SPRibbon – just do a CTRL+. and choose to add the ‘using’ statement shown :
Obviously – you’ve probably guessed that you don’t just include xxxxxxxxxxxx in the TrimById method. This will correlate to an entry in the following file :
C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14TEMPLATEGLOBALXMLCMDUI.XML
- Open the file in Internet Explorer – it’s pretty big, eh !
- Do a search for “Ribbon.EditingTools.CPEditTab.Groups”
- The sub-nodes are the ones you’re interested in – a collection of GROUP nodes.
- The groups should look familiar – they correlate to the panes on the ribbon pane :
- It follows that you can “TrimById” using these ID’s and the ribbon will be trimmed (!)
AND – to go further than that – you can TRIM off individual buttons – not just the group as a whole.
Within the GROUP node, is a CONTROLS collection. If we expand the CPEditTab.Font node – you’ll see each of the buttons as shown on screen.
So – the code to hide the Bold, Underline and Italic buttons – would be :
Easy, eh !? Well – let’s go put it to use.
Deploy & use the control.
The amazing new SharePoint oriented functionality within Visual Studio has meant that deployment of this control could NOT be easier – I dare you to find an easier way to do it !!!
- Right-click on the Project node – and click Deploy.
- Ta-da ! See – I told you it was easy !
Then – we need to reference the control from within the Master Page :
- Use SharePoint Designer 2010 to open your chosen Master Page
- Add the following tag at the top of the Master Page
<%@ Register TagPrefix="SPR" TagName="RibbonItemHider" src="~/_controltemplates/SPR.Utilities/RibbonItemHider.ascx" %>
Now – we just need to “use” the tag somewhere in the Master Page. I added it immediately after the start of the <BODY> tag – pretty sure you could add it anywhere within the HTML body – not in the HEAD section.
<SPR:RibbonItemHider id="RibbonItemHider" runat="server" />
Try it out
When you go back to SharePoint – and change the Master Page to be the one you’ve just edited – you should see that the ribbon has indeed been “trimmed” – cool, eh !?
See – the Bold, Italic and Underline are gone !
A few hoops to jump through – but you can now trim down other functionality using the same framework – if you’re needing to restrict what editors, authors and so forth are able to do.
Some GOOD uses of this are (for example) :
- Remove the Edit HTML button – don’t let Content Authors muck with it.
- Take out the SuperScript and SubScript buttons – and/or Strikeout
- Remove the Paragraph tab – and then force the visual layout via CSS.
Pretty easy to achieve – and quite easy to narrow down what Content Authors are able to do.
Hope this was of use to you – or at the very least, a little bit ‘interesting’…