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:

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:

6 Comments on “HowTo: Show ribbon button depending on Application running

  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

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.