Dynamics CRM - Show Ribbon Button On One Model Driven App

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,

  1. Create new solution and add Account entity into it.
  2. Create a Javascript web resource and add code given at end of the blog. I will explain code in the end.
  3. Open Ribbon workbench and Right click on Assign button from Account form and click on Customize Button
  4. Add Enable rule and call Javascript function
  5. Associate this Enable rule to Common
  6. 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

  1. We have to use Promise to return the result correctly. Read more about promis in Javscript by Debajit here
  2. getCurrentAppProperties() API which returns the Model Driven App properties.

Result

Sales Hub

Dynamics CRM Show Ribbon button on one Model Driven App

Customer Service

Dynamics CRM Show Ribbon button on one Model Driven App

Hope this helps!