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>

            <!–Display the button only for Customers–>

             <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

  • shobhika

    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.

  • Pradeep

    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?

  • Xitij Thool

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

    • Xitij Thool

      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

  • samitha

    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.

Return to Top ▲Return to Top ▲