Blog, Development

Showing ribbon button based on the result of async operation

Earlier when I had a task to show/hide button based on the result of data-retrieval operation I used following approach – declared variable, made synchronous call to endpoint, set that variable inside the callback and returned true/false result at the end of validation function. It worked but if the operation was long-running it caused blocking of UI (that is bad and not user-friendly at all). In this post I’ll show how to work around this situation.

Example I will use today is classic – when user doesn’t have specific role (System Administrator in my case) button is not available for user. Also I developed and tested everything on v9.0 so it could require change on your environment if its of earlier version.

1. Please check code with comments:

2. Ribbon Configuration – there are not a lot of special things to mention but to make it compatible with UCI formContext should be passed inside “Custom Enable Rule”:

Detailed description how to work with formContext in UCI-compatible code you can find here.

UPD approach mentioned will work for buttons that are located on the form. If you want to refresh ribbon of grid check this post.

6 Comments on “Showing ribbon button based on the result of async operation

  1. Hi Andrew

    It works fine! But, if the button is inside a dropdown list, the enable rule code is executed when you click on the arrow and you have to click again to make it appear or hide the button, would there be any way for the enable rule to be executed when you load the form ?

    Thank you

    1. Alberto,
      That’s an interesting usecase. To be honest I haven’t tried that so I don’t have an answer ATM.

  2. In my case I had a problem because the results are fetched only once. So for dynamically changing values I had to modify it. In my version, I left out the ‘isAsyncOperationCompleted’ part and instead, compared the flag result of the query with the current one, update it if necessary and refresh the ribbon if they’re different. That way, I avoid infinitely refreshing the ribbon while keeping the value updated.

    1. Kevin,
      I agree. My scenario is applicable when you don’t have dynamically changing values. Security roles is not the thing that keep changing every minute 🙂
      Thanks for comment and interest to my post.

  3. Has anyone had issues with this technique after updating to CRM 9.0? We are using this in a CommandBar button (aka subGrid ribbon) and are finding refreshRibbon(true) — which is documented to refresh all buttons on all command bars — does not refresh our button.

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.