Create Dynamic Ribbon Controls
Microsoft Dynamics CRM 2011 uses Ribbons (also known as Fluent UI) to provide a user interface to display the right commands at the right time. Ribbons also provide a very powerful and flexible way of extending the ribbon functionality to meet your requirements.
I’m going to show you how to create dynamic Ribbon controls. You will typically use dynamic controls so that your ribbon elements can react to changes in data or context. ComboBox, DropDown, FlyoutAnchor, MRUSplitButton and SplitButton controls can contain other collections of controls. These controls can be created with static definitions at design time or they can be populated dynamically using a JavaScriptFunction that uses a Script Web Resource.
In this article I will demonstrate how to dynamically create ribbon Button elements and populate them inside a FlyoutAnchor. The end result will look like this:
The procedure uses these steps:
- Define the Parent Control
- Define CommandDefinitions
- Create a Script Web Resource
Define the Parent Control
Once you determine the location where you would like to create a dynamic menu, create a CustomAction to add the CommandUIDefinition as shown below.
The value of the CustomAction Location attribute shows that I have chosen to add a FlyoutAnchor to the Tools group of the Basic Home Tab.
Set the value of PopulateDynamically attribute to true. This attribute will tell the rendering engine to populate the content of the menu dynamically. Specify the Id value of the CommandDefinition you will create in the next step to the PopulateQueryCommand attribute. The next step will show how to define the CommandDefinition.
Define CommandDefinitions
There are two CommandDefinition elements:
- ISV.PopulateMenu: In addition to the “command” property used by standard controls, dynamically populated controls utilize another command “PopulateQueryCommand” which is responsible for retrieving the dynamic data.
- This CommandDefinition will control what elements will be shown.
- It uses the Jscript function named “DynamicMenu”.
- ISV.SearchCommand
- This CommandDefinition will control what actions should be performed when the dynamically generated control elements are clicked.
- It uses the Jscript function named “Search”.
Each CommandDefinition contains a JavaScriptFunction action that specifies the Script Web resource. Specify the unique name of the Script Web resource with $webresource: prefix to the Library attribute.
Important:
For both CommandDefinition elements, you must use the CrmParameter with the Value attribute set to “CommandProperties”. This object will be used later in the script web resources to read which dynamically generated button is selected and to set the dynamically generated ribbon xml.
Create a Script Web Resource
Create a Script Web resource named new_dynamicmenupopulator.js. Set the contents of this JScript library to the following code:
This library contains two functions:
- DynamicMenu
- Search
DynamicMenu Function
The DynamicMenu function defines the XML for a Menu element containing Button definitions. In this example, there are two buttons defined within a MenuSection element. Each Button uses the following attribute values:
- ISV.Dynamic.Button1
- Command: ISV.SearchCommand
- Sequence: 20
- LabelText: Test Button1
- Alt: Test Button1
- ISV.Dynamic.Button2
- Command: ISV.SearchCommand
- Sequence: 30
- LabelText: Test Button2
- Alt: Test Button2
Both of these buttons are configured to use the ISV.SearchCommand created in the previous step.
Search Function
The Search function is called when buttons are clicked at run time. Because the CommandDefinition you created in the first step passes the value “CommandProperties“, this object is passed through and you can read the SourceControlId property to determine which of the dynamically generated buttons was clicked and perform the appropriate action.