Business Requirement
Today one of my colleagues asked, "How can I Show/Hide a ribbon button based on Model Driven APP?" I thought of exploring this use case by myself and wanted to share it with you.
Dynamics CRM 365 recently released a new JavaScript API to get Model Driven App properties which will help us to get App ID and other details. We are going to use App Id to write our logic to either return true or false from Javascript function.
I am going to show Assign button on Account form in Sales Hub and hide same button in Custom Service.
Here are step by step guide to achieve this Business requirement,
- Create new solution and add Account entity into it.
- Create a Javascript web resource and add code given at end of the blog. I will explain code in the end.
- Open Ribbon workbench and Right click on Assign button from Account form and click on Customize Button
- Add Enable rule and call Javascript function
- Associate this Enable rule to Common
- Publish the changes and refresh the form on both App and voila!
Javascript Code
function ShowHideRibbonButtonBasedOnModelApp() {
return new Promise(function(resolve, reject) {
debugger;
var globalContext = Xrm.Utility.getGlobalContext();
globalContext.getCurrentAppProperties().then(function(object) {
var found = false;
if (object.appId == '89603f8e-0d98-ea11-a811-000d3a0a7553') found = true; //Sales Hub
else if (object.appId == '2a96fdac-0d98-ea11-a811-000d3a0a7553') found = false; //Custom Service
else found = true; //other Model Driven Apps
resolve(found); //promise to retun true or false
}, function(error) {
reject(error.message);
console.log(error.message);
});
});
}
Key Points
- We have to use Promise to return the result correctly. Read more about promis in Javscript by Debajit here
- getCurrentAppProperties() API which returns the Model Driven App properties.
Result
Sales Hub
Customer Service
Hope this helps!