Thursday, 5 February 2015

Control Ribbon Visibility (Show/Hide) based on Specific Form in CRM 2011/2013/2015 Multiple Entity Forms

CRM Supports Multiple Forms and we can put ribbon (button in CRM) or Command in the CRM Forms as well. What if we have requirement to show or hide the ribbon based on Form. In this post I will give example of the code to control ribbon visibility.

Scenario

I have scenario that I have 3 Forms of Account Form.

Now, I want to place a ribbon applicable for Account Form, but only for Partner Form. So when the users are in different form, other than Partner Form, this ribbon should not be available.

The Code

The code is in JavaScript code and it’s pretty simple.

function controlFormRibbon() {
var formLabel;
var currForm = Xrm.Page.ui.formSelector.getCurrentItem();
formLabel = currForm.getLabel();
//this code to show the ribbon if only the current opened form is Partner,
//otherwise remain disable/hidden
if (formLabel == "Partner") //change this with your own specific form name
{
return true;
}
else
{
return false;
}
//you can also apply the different/reverse rule here, for example you want to hide if form is Partner
}

Ribbon Workbench Favor

Here, I need a favor from my favorite tool.

I have solution with the ribbon image and script now I just want to place a ribbon and set the JavaScript there.

I create a Button in the Form section, give the Command, rename the Id, assign the Images to the Ribbon.

Then I rename the Label to ‘Partner Only’ that you can see in the final result showing this label Text.

Then Here is the Command.

I apply a Rule here to EnableRules.

This is my rule:

*You can change the Default to True/False to set the default visibility, but in my script I have set other than Partner, will hide this ribbon button, so I don’t bother to change to False.

The key here is just referring the correct FunctionName under the correct Library (your Web Resource where you kept the script) and set parameters if any.

Note: The display rules doesn’t support CustomRule, so just apply it to the EnableRule to show/hide.

Result

I can navigate using Form Selector

*My Partner Form

*My Account & Information Forms

As you can see the ribbon is there if I choose the form to Partner, otherwise it’s hidden.

I tried using this method and at one time I think it initially worked, but after making some additional changes within CRM, it stopped working. It took me a while to figure out the problem. Read https://social.microsoft.com/Forums/en-US/df1f3680-13e8-4a80-ae33-aec16dc89497/crm-2011-get-form-name-using-javascript?forum=crmdevelopment which further down says that Xrm.Page.ui.formSelector.getCurrentItem() only works in certain conditions. If a user only has access to one form, this does not work.

I've not found a similar solution that does work, though. In my instance the user's I'm trying to control the ribbon visibility for only use this one entity form anyway and they all have a custom security role in common, so I'm just going to create a custom entity that this role has access to and use a 'Entity Privilege Rule' to that entity to show/hide buttons.https://ribbonworkbench.uservoice.com/knowledgebase/articles/76681-hide-a-standard-out-of-the-box-button-based-on-a

It was either this or create a custom field on the form as suggested in the discussion I linked to.