Friday, September 16, 2011

MSCRM 2011 Ribbon dynamically display the save button.


Microsoft Dynamics CRM 2011 Ribbon is great once you get to know how to work with it. 
At the beginning it looked too complicated, there are too many tags to write and it is difficult to understand how to place the button in the right place. 
The good news are after understanding the why it works, it all makes sense.
They are very flexible and you can do all most everything in a supported why.

This blog shows how to remove and show Ribbon save buttons based on some logic.

In MSCRM 4 it can be done using unsupported code
Something like: 

var toolBarButtons = document.all.mnuBar1.rows[0].cells[0].childNodes[0].childNodes;
and then iterate over all the buttons till the save button found and removed / showed when needed.


In MSCRM 2011 there is supported and much more elegant solution:

Generally speaking you should know these steps before starting (Copied from: http://msdn.microsoft.com/en-us/library/gg334532.aspx):
1.     Prerequisites: This section describes the specific requirements and resources you need.
2.     Export: Create and export a solution containing just the solution components you need. For more information about creating and exporting a solution see the application Help and Export the Ribbon.
3.     Identify the IDs for existing items: Examine default ribbon definitions. The walkthrough provides the XML you need, but it is important that you understand the relationships to existing data in the default ribbon definitions.
4.     Create the script Web resource: Walkthroughs that add controls to the ribbon require a JScript Web resource to perform an action. For more information about creating JScript Web resources, see the application Help and JScript Libraries for Microsoft Dynamics CRM.
5.     Edit the customizations.xml file: This section leads you through a series of cumulative steps editing nodes within the <RibbonDiffXml> (RibbonDiffXml)element. The final step shows you the entire modified RibbonDiffXml element in the customizations.xml file.
6.     Import and publish the solution: Compress the modified solution files and import those files to update your solution. For more information about compressing and importing a solution, see the application Help and Import the Ribbon.
7.     Test and verify that the requirements are met: In this step you confirm that the requirements specified in the prerequisites section are met.


We'll twist it a little bit to meet our requirement.

1) Replace the CommandDefinition that descripes the save button with new one. We'll copy it from incidentribbon.xml and place it in the exported customization.xml.

      <CommandDefinition Id="Mscrm.SavePrimary">
        <EnableRules>
          <EnableRule Id="Mscrm.AvailableOnForm" />
          <EnableRule Id="Mscrm.CanSavePrimary" />
          <EnableRule Id="Mscrm.ReadPrimaryPermission" />
          <EnableRule Id="Mscrm.Form.incident.MainTab.CustomRule.EnableRule" />
        </EnableRules>
        <DisplayRules>
          <DisplayRule Id="Mscrm.CanSavePrimaryEntityType" />
        </DisplayRules>
        <Actions>
          <JavaScriptFunction FunctionName="Mscrm.RibbonActions.saveForm" Library="/_static/_common/scripts/RibbonActions.js">
            <CrmParameter Value="PrimaryControl" />
          </JavaScriptFunction>
        </Actions>
      </CommandDefinition>

2) add new Enable Rule which indicates should it be shown or not
<EnableRule Id="Mscrm.Form.incident.MainTab.CustomRule.EnableRule">
     <CustomRule FuncationName="EnableButtons" Library="$Webresource:new_incident_library" />
</EnableRule>

3) Now in your code you'll be able to decide would the save button be shown or not by defining the logic in the function EnableButtons. Whenever you want to change the display make sure the function returns true / false as expected and refresh the ribbon by using: 
Xrm.Page.ui.refreshRibbon()

The thing to remember here is that you can change the current setting of almost everything in the ribbon by redefining them.

No comments:

Post a Comment