Hiding & Disabling ribbon items in Project Server, Part II
By blurg64
In the last post, we covered a few things you will need to know when hiding or disabling ribbon buttons in PWA. In this post we will look at how to actually hide the buttons.
Where to start?
Irrespective of whether you wish to hide or disable a button in the ribbon, you will need to follow the same basic process by building a solution in Visual Studio that will deploy your ribbon customisation.
To start, open up Visual Studio and choose to create an Empty SharePoint Project.
Enter a name and then click on OK. You will then see the SharePoint Customisation Wizard, enter the local site you wish to use for debugging and choose to ‘Deploy as a sandboxed solution’.
On clicking finish, Visual Studio will create a solution ready to be customised.
To start with we need to create a feature, this is a logical container for our customisation and allows the user to turn on and off the customisation by activating and deactivating the feature. To do so, right click on Features in the solution explorer and choose Add Feature.
A feature will be created and Visual Studio will show a page where you can enter information about the feature such as the name, description and the scope. In this case we are going to choose Web (for an individual site) as we want this change to deploy only to the /PWA site and not all the children sites.
Next we want to add an element to the solution, this is where the real work is done and contains the XML that will be used to configure our ribbon.
To add an element, right click on the project name, choose** _Add_**,** _New Item_**
In the dialog that is displayed, scroll down and select Empty Element and give it a name.
Once you click add, the element will be created.
Now the empty element is created, all that is needed is to add the relevant XML to either disable or remove the button.
Disabling a button
For this example, we shall be disabling the EPT Change button from the Project Centre ribbon. To do so we need to know the ID of the item as defined in the ribbon so we can build up some XML that defines what we want to do to that item. To find the ID, we need to look through the PWARibbon.xml file, which can be a bit daunting, but after a while you will understand the structure and finding the Id’s will become simple.
SharePoint ribbon customisations use a defined XML schema which describes the structure and behaviour of the ribbon and the items that make it up. In order to disable the EPT Change button, we need to override the current structure and behaviour to change the configuration of what the button will do, in this case, pointing to a command that doesn’t exist. In doing so, the ribbon will disable the button for us, giving us the desired effect.
To get the various attributes of the button, the PWARibbon.XML is your friend, containing all the configuration information you will need. In this case I have taken the configuration of the button and changed the command to point at a non existent command, which will cause the button to be disabled. This XML then needs to be put in the Element.xml file ready to be built, but more on that later.
[sourcecode lang=”XML”]
<CustomAction
Id="Ribbon.ContextualTabs.ProjectCenter.Home.ChangeProjectType.Change”
Location="CommandUI.Ribbon”
Title="Disables the EPT Change button in the Project Center Ribbon">
<Button
Id="Ribbon.ContextualTabs.ProjectCenter.Home.ChangeProjectType.Change”
Command="ChangeReplacement”
Sequence="10”
Image16by16=”/_layouts/$Resources:core,Language;/images/ps16x16.png”
Image16by16Top=”-112”
Image16by16Left=”-190”
Image32by32=”/_layouts/$Resources:core,Language;/images/ps32x32.png”
Image32by32Top=”-352”
Image32by32Left=”-96”
LabelText="$Resources:pwafeatures,WEBPARTS_PROJECTCENTERPART_CM_CHANGE”
TemplateAlias="o1”
ToolTipTitle="$Resources:pwafeatures,PAGE_PDP_CM_CHANGE_WORKFLOW”
ToolTipDescription="$Resources:pwafeatures,SUPER_TOOLTIP_CHANGE_PROJECT_TYPE”
/>
[/sourcecode]
Removing a button
The XML code required to remove a button is simpler. Instead of defining the XML for the whole button, all that is required is to redefine the CommandUIDefinition as per below.
[sourcecode lang=”XML”]
<CustomAction
Id="Ribbon.ContextualTabs.ProjectCenter.Home.ChangeProjectType.Change”
Location="CommandUI.Ribbon">
[/sourcecode]
When PWA parses the XML it uses this to remove the button as key items are not defined. Once again, all that is required to remove the button is to move this XML into the Elements.XML file and save ready for building.
In both cases, if you wish to hide or disable more than one button, you will need to duplicate the Custom Action mark up and change as necessary for each button.
How to Build
Finally once the configuration is completed, all that is required is to build and deploy the solution. To do so, right click on the project name and choose build, if everything is ok, you should see something similar to this in the build output:
If the build fails, then there may be a problem in your code. I have packaged the code up I used to build this post into a zip file which is available from my Skydrive account so you can compare or copy.
Once the build is successful you can deploy the solution by right clicking on the solution name and choosing deploy. VS 2010 will then deploy the solution to the site you entered for debugging and activate the solution in the site collection.
Once the solution has been activated, you should see the changes to the ribbon in Project Center.
As you can see from above, creating a custom feature to hide or disabling buttons in the ribbon is relatively simple once you have the basic structure in place. Hopefully this post has explained how to build these customisations for use in your next project or internal implementation.