Enable Ribbon Button using rules in Dynamics CRM 2011

Zhen Yuwang, 01 April 2012

When we configure the ribbon buttons in Dynamics CRM 2011, we can define specific customized rules to enable or disable them in RibbonDiffXml. There are two places we need to change. One is in “/RuleDefinitions/EnableRules/EnableRule” to define the rule controlling when ribbon button should be enabled. Another one is in “/CommandDefinitions/CommandDefinition/EnableRules/EnableRule” to link this rule and a command definition. In this post, I will show you how to enable a ribbon button based on a conditional “OR” rule which will look at two values on a form.

First of all, we need to add Application Ribbon and the entity whose form we will put button in, to a Solution. Export that solution.

 Enable Ribbon Button using rules in Dynamics CRM 2011

Then, extract it and open customizations.xml in a XML editor like Visual Studio 2010.

Find the position of RuleDefinitions in RibbonDiffXML of target entity.

Add 2 ValueRules like this into EnablueRules:

<ValueRule Field="mag_enablebutton" Value="true" />
<ValueRule Field="mag_enableoptionset" Value="100000000" /> 

The first rule is for a Radio Button with possible value “true” or “false”. The second one is for an Option Set with possible value “100000000” or “100000001”.

Next we add OrRule to make these 2 ValueRules could be evaluated together

            <EnableRule Id="Mag.Form.mag_testentity.TestButton.Enable">
              <OrRule> 
                <Or> 
                  <ValueRule Field="mag_enablebutton" Value="true" />
                </Or> 
                <Or> 
                  <ValueRule Field="mag_enableoptionset" Value="100000000" />
                </Or> 
              </OrRule> 
            </EnableRule> 

Following is linking this EnableRule to the CommandDefinition of our Ribbon Button, using their Id.

<CommandDefinition Id="Magnetism.mag_testentity.form.CustomGroup.Command">
            <EnableRules> 
              <EnableRule Id="Mag.Form.mag_testentity.TestButton.Enable" />
            </EnableRules>

Save and Zip files, import back to system.

Finally, what we get is this logic. If any one of conditions below is satisfied, the ribbon button will enable; only both conditions are not meet, disable that button.

Check it in our Dynamics CRM 2011:

 Enable Ribbon Button using rules in Dynamics CRM 2011

Enable Ribbon Button using rules in Dynamics CRM 2011

Enable Ribbon Button using rules in Dynamics CRM 2011