Sometime you want to add a custom button to the ribbon, by default you can only select if you want to show it on the main grid, the main form or a subgrid. But what if you want to hide it on some subgrid. By using enable rules you can build this.
Write your custom ribbon code just like your just to and simply add a new function to your file named “HideBtnSubgrid”
We are going to add 3 parameters to this function
- primary Control
- You need this to get the form context from your current form
- entityName
- This is the entity where you don’t want to show the button
- formName
- This is the form name where you don’t want to show the button
First we are going to check if you got the form context. If we don’t have the formcontext we can’t check on which form we are
const formContext = primaryControl;
if (!formContext) {
console.log(“Form context not available”);
return false;
}
Secondly we are going to check if we are on a form from the right entity
const currentEntityName = formContext.data.entity.getEntityName();
console.log(“currentEntityName”, currentEntityName);
if (currentEntityName !== entityName) {
console.log(“currentEntityName not “+entityName);
console.log(“Button should be visible”);
return false;
}
If we know that we have the formcontext and are on a form from the right entity we can check if we are on the form where we don’t want to show the button.
const currentFormName = formContext.ui.formSelector.getCurrentItem().getLabel();
console.log(“currentFormName”, currentFormName);
if (currentFormName !== formName) {
console.log(“currentFormName not ” + formName +” , button should be visible”);
return false; // Different form, do not hide the button
}
In the end your function will look like this:

Now you can go to the Ribbon Workbench in the Xrm Toolbox where you registered you custom Button
Add a new enable rule, fill in the name of your function and create the 3 parameters. The primary control will be of type “CRM Parameter”, the other 2 parameters will be text. After this you can publish your ribbon and you will see that the ribbon will not be shown on the selected form.

Plaats een reactie