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.
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
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.
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”.
- This CommandDefinition will control what actions should be performed when the dynamically generated control elements are clicked.
- It uses the Jscript function named “Search”.
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 named new_dynamicmenupopulator.js. Set the contents of this JScript library to the following code:
This library contains two functions:
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:
- Command: ISV.SearchCommand
- Sequence: 20
- LabelText: Test Button1
- Alt: Test Button1
- 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.
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.