Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications


How To Enable and Disable Ribbon Buttons in CRM 2011

Post Author: Joe D365 |

One of the cool features in CRM 2011 is the ability to customize the Ribbon bar (on the entity form as well as in the Lists). One of the most common requirements is to have custom buttons available on a form ribbon bar. This can be done by adding the Custom Button Xml to the RibbonXml element of the customizations xml for an entity. You can specify what action you need to do on click of this custom button in a JavaScript web resource and specify this in the Command Definition element for this button. So without further ado, let's go over how to enable and disable ribbon buttons in CRM 2011!

Let us take the example of adding a custom button to the Account form. Here is the screenshot of the Account form which represents the location of the Custom button to be added to the form

Enable and Disable Ribbon Buttons in CRM 2011

Here are the steps needed to enable/disable a custom ribbon button depending on the values of a certain field on the form

- First create a new solution and add the entity (account in this case) for which you want to customize the ribbon bar.

How to Disable Ribbon Buttons in CRM 2011

- Export the solution zip file and extract the files. This is how the Ribbon Definition looks without any customization

<RibbonDiffXml>

        <CustomActions />

<Templates>

<RibbonTemplates
Id="Mscrm.Templates">RibbonTemplates>

Templates>

        <CommandDefinitions />

<RuleDefinitions>

<TabDisplayRules />

<DisplayRules />

         <EnableRules />

RuleDefinitions>

<LocLabels />

RibbonDiffXml>

- Then add the custom button definition in the RibbonXML. Below is the code which defines a custom button to be added to the "Data" group on the main tab of the Account entity's form

<RibbonDiffXml>

     <CustomActions>

         <CustomAction
Id="GT.account.Form.ExportData.CustomButton"

Location="Mscrm.Form.account.MainTab.ExportData.Controls._children"
Sequence="2">

            <CommandUIDefinition>

             <Button
Id="GT.account.Form.ExportData.CustomButton"
Command="CustomButton"

LabelText="Custom Action"
ToolTipTitle=""
ToolTipDescription="Perform a Custom Action"

TemplateAlias="o1"
Image16by16="/_imgs/ribbon/customaction16.png"

Image32by32="/_imgs/ribbon/customaction32.png"
Image16by16Class="ms-crm-Upgraded-Ribbon-Image16"

Image32by32Class="ms-crm-Upgraded-Ribbon-Image32" />

CommandUIDefinition>

         CustomAction>

     CustomActions>

<Templates>

<RibbonTemplates
Id="Mscrm.Templates">RibbonTemplates>

Templates>

Here "Location" is the attribute where you specify the group you want the button to be displayed in. This can be found out by using IE developer tool to find another element in the same group and use that as a reference. And the icons for the button (16by16 and 32by32) can be any custom icons that you can add as a web resource in the CRM (In the above example, we just used the out-of-box available icons on the CRM server). Here is how to reference the icons defined as web resources. The Button definition with custom icons could be defined as follows:

<Button
Id="GT.account.Form.ExportData.CustomButton"
Command="CustomButton"

LabelText="Custom Action"
ToolTipTitle=""
ToolTipDescription="Perform a Custom Action"

TemplateAlias="o1"
Image16by16="$webresource:new_CustomIcon16"
Image32by32="$webresource:new_CustomIcon32"

/>

Here "new_CustomIcon16" is the schema name of the web resource

- Now define an Enable rule for the button using the following code. In the below example, we want to enable this button only for Accounts of type "Customer". This is determined by the attribute "Relationship Type" (Schema name: customertypecode) on the account.

<CustomActions>

     <CustomAction
Id="GT.account.Form.ExportData.CustomButton"
Location="Mscrm.Form.account.MainTab.ExportData.Controls._children"
Sequence="2">

        <CommandUIDefinition>

         <Button
Id="GT.account.Form.ExportData.CustomButton"
Command="CustomButton"
LabelText="Custom Action"

ToolTipTitle=""
ToolTipDescription="Perform a Custom Action"
TemplateAlias="o1"

            Image16by16="/_imgs/ribbon/customaction16.png"
Image32by32="/_imgs/ribbon/customaction32.png"

Image16by16Class="ms-crm-Upgraded-Ribbon-Image16"
Image32by32Class="ms-crm-Upgraded-Ribbon-Image32" />

        CommandUIDefinition>

     CustomAction>

CustomActions>

<Templates>

<RibbonTemplates
Id="Mscrm.Templates">RibbonTemplates>

Templates>


<CommandDefinitions>

     <CommandDefinition
Id="CustomButton">

    <EnableRules>

         <EnableRule
Id="Mscrm.Isv.account.Form.CustomButton.Enable"/>

         EnableRules>

         <DisplayRules>

         <DisplayRule
Id="Mscrm.Isv.account.Form.Group0" />

         DisplayRules>

         <Actions>

         <JavaScriptFunction
Library="$webresource:new_CustomAction"
FunctionName="CustomActionFunction">JavaScriptFunction>

         Actions>

     CommandDefinition>

CommandDefinitions>

<RuleDefinitions>

<TabDisplayRules />

<DisplayRules>

     <DisplayRule
Id="Mscrm.Isv.account.Form.Group0">

            <FormStateRule
State="Create"
InvertResult="true" />

         DisplayRule>

        DisplayRules>

        <EnableRules>

         <EnableRule
Id="Mscrm.Isv.account.Form.CustomButton.Enable">

            

             <ValueRule
Field="customertypecode"
Value="3"
Default="0" />

         EnableRule>

        EnableRules>

RuleDefinitions>

<LocLabels />

RibbonDiffXml>

The Command Definition is the section which specifies which rules to use for enabling/displaying, showing/hiding and custom actions (JavaScript to be executed) on click of the button. Then the individual Display and Enable Rules sections are defined under the Rule Definitions section. In the above example for the Display Rule, we make the button to be displayed on all forms except create (indicated by the "InvertResult = true" attribute of the Display Rule section). While for Enabling, we specify the button to be enabled when the Relationship Type (customertypecode) value is 3(customer). In all other cases, we disable the button including when there is no value specified for this field (this is indicated by the "Default=0" attribute which specified the return value of the rule to evaluate to false if no value is specified for the field – "customertypecode")

- Now, re-zip the customization files and import the solution back to CRM and remember to publish the customizations.

- Now, open an existing account to find the button disabled when there is no value or value other than customer specified in the Relationship Type field

- When you change the value to "Customer" and save the record, the button will appear enabled as shown in the screenshot below:

- If you try creating a new account, you will notice that the button is not displayed in the "Data" group.

This is because of the Display Rule definition to not display on the Create form.

This post only covered how to enable and disable ribbon buttons in CRM 2011. There is lot more that can be customized and personalized on the Ribbon for CRM 2011 depending on the ribbon location itself (Grid Vs Form). Hope this helps someone out there!!

Happy CRM'ing

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

8 comments on “How To Enable and Disable Ribbon Buttons in CRM 2011”

  1. I want the button to be enabled when the Save button on the opportunity for is clicked. And the form is saved.
    What Value Rule can be written for the same.

  2. I want the button on the entity "account" and if once i clicked that button, it must redirect to another entity/form. suppose, if i click on button in account form, it must redirect to contact entity form. what value rule can be written for that?

  3. Can you help us out for the Activities whose Status is “Completed” and to Enable the Custom Buttons.

    1. I had one condition to enable the button only when it is not Creating state . So tried one rule for Existing one and Mark Completed one they did not work only your specified rule worked that is

  4. I want to know what all should i include in the ID of custom action kindly reply with an example

    i know like this [solution identifier].[entity].[ribbon].[function].[element name].
    For example: MyISV.account.form.SendToOtherSystem.Button.
    so in the above statement what is that solution identifier, function.

  5. We have an issue when apply these sort of changes to our buttons. If you open a page that has an enable or display rule, you can actually click the buttons for a split-second before the rules are applied. At least this is the case in CRM 2011.

    So how do you block them from being enabled before the enable rules are executed that disable them? Seems like a design flaw with how the enable rules are being applied if the user is able to click on them before they are disabled.

    I didn't believe it was possible at first, but after working with it for a bit, I can recreate this issue again and again. How do you work around this? I'm going to try disabling them via JavaScript to see if I can block them myself.

  6. HI, I used the same Value Rule in CRM 2013, but instead of Disabling the Button, it is hiding the button. So this is correct or I did something wrong.? Kindly reply with the same. I want to disable the button and not to hide the button.

  7. HI, I used the same Value Rule in CRM 2013, but instead of Disabling the Button, it is hiding the button. So this is correct or I did something wrong.? Kindly reply with the same. I want to disable the button and not to hide the button.

PowerObjects Recommends