Blog, Development, Howto

HowTo: Show ribbon button depending on Application running

Several days back I saw an email from Steve Mordue MVP asking if it is possible to filter ribbon elements based on application like it’s implemented for entities, views and forms. Answer was that at the moment there is no easy way to do it. In this post I will help Steve to resolve his issue using JavaScript and Ribbon Workbench.

JavaScript

First of all to define what application is running getCurrentAppProperties method can be used. The issue that arises that it returns Promise and not the result itself so to resolve it I’ll use approach similar to this post – by default button is not shown and visibility of it is set in “success” handler of promise. Here is the code:

var AB = AB || {};

AB.Ribbon = (function() {
    //defaulting button visibility and promise completion to false
    var isButtonEnabled = false;
    var isPromiseCompleted = false;

    function IsButtonEnabled(formContext, appUniqueName) {
        //if promise was completed already that result is just retuned
        if (isPromiseCompleted) {
            return isButtonEnabled;
        }

        //getting parameters of current running application
        formContext.context.getCurrentAppProperties().then(function (appProperties) {
            //Marking promise as completed
            isPromiseCompleted = true;

            //if appid passed as a rule parameter is equal to current running application
            if (appProperties.uniqueName === appUniqueName) {
                //button is marked as enabled and ribbon is refreshed
                isButtonEnabled = true;
                formContext.ui.refreshRibbon();
            }
        }, function (error) {
            //who knows what could happen
            isPromiseCompleted = true;
            Xrm.Navigation.openAlertDialog({ text: error.message });
            });

        return false;
    }

    return {
        IsButtonEnabled: IsButtonEnabled
    };
})();

Create JavaScript webresource with provided script.

Ribbon Workbench

Add a button, add a command and point button to command created, add Enable Rule to command, add custom rule to it:

Save and publish ribbon. The next question is – where can I get application unique name from? Open Model Driven App Designer, open Properties and scroll to bottom. There you can find “Unique Name” field.

Demonstration

Here are 2 different apps with the same forms opened. In first application button is available. In second – it’s not:

10 Comments

  1. Nice Article Andrew,
    The Classic Ui has the unresolved promise for the formContext.context.getCurrentAppProperties() call due to which it will neither lands up in success nor in error method.
    If the customer still needs both the interfaces i.e. Classic UI as well as Unified Interface then how to handle this unresolved promise in Classic UI?

    1. Anubhav,
      I believe you can do following:

    2. Open support ticket with Microsoft asking to fix their bug (because unresolved promise looks for me as a bug)
    3. As a workaround you can extract appid from the top window url and use it to obtain app name or other properties you need
    4. Andrew

  2. Hello,

    This is awesome thank you so much for this. It works perfectly fine. I do have a question:

    It works perfectly fine for forms, thank you so much

    . However, I also need to hide button when seeing the records from a view. In order to do so I just, added the enable rule to the specific buttons (commands) but it does not work.

    Did you manage to achieve this?

    1. Hello Dimitri,
      I haven’t done that for the grid. I believe that the main difference is what is passed to the function when it is registered for the grid. Can you please post a screenshot that shows customization of your button and did you perform any change in JavaScript?
      Thanks,
      Andrew

  3. Hello Andrew,

    Thank you for your quick reply.

    In fact in I am trying to hide (based on the app) the (Run Report) OOB button (I did not any customization, just added the enable rule). More precisely :
    1) I just opened Workbench
    2) Identified the command on the form Mscrm.ReportMenu.Form and associated the enable rule (associated with the web Ressource containing the exact code you indicated – I am not a technical consultant just followed the steps you indicated, no change on the Javascript 🙂
    3) Published —> Everything fine

    I followed the same exact step because I wanted to hide it when viewing multiple records so I did the same steps

    1) I just opened Workbench
    2) Identified the command on the form Mscrm.Mscrm.ReportMenu.Grid and associated the same enable rule (associated with the web Ressource containing the exact code you indicated)
    3) Published —> Unfortunately (the run report button is visible on all apps).

    Please let me know if you need anything else.

    Thanks in advance,

    Dimitri

    1. Dimitri,
      I will take a look. I believe that script can have differences.
      Thanks,
      Andrew

  4. Andrew, good morning. I’m with a big problem about ribbon workbench.

    Maybe you can help.
    We have two enviroments (dev and prod).
    So, what do we do? Each month, a solution in dev.
    End of month: put dev solution in prod.

    But with this new UCI, everytime that we do this, buttons already removed of forms in dev, it’s back in prod.

    I don’t know why (we delete button, command or hide it). Dev always is ok, but prod keeps come back.

    Do you know what can be?

    Thanks!!

  5. Hi Andrew,

    Do you know if this is possible on the Advanced Find ribbon? It appears the javascript file specified on the Enable Rules Custom Rule doesn’t load there. The Enable Rules doen’t appear to have a ‘Custom Rule’ type at all.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.