Monday, April 27, 2015

Hide Ribbon Button based on View selected

Sometimes we need to hide some button based on view selected.
CRM doesn’t have this functionality, and there is not supported way to achieve this. With small unsupported JavaScript code you can easily hide ribbon buttons based on view selected.
To do this, need to add Enable rule to button command.
I am using Ribbon workbench to add Enable Rule.
1.      If want to hide custom button then select button command, and click on Enable rules

If want to hide existing CRM button then need to customize button command, and then add Enable rule.




2.      Add new Enable rule to command




3.      Add Step to Enable rule and select Custom JavaScript Rule


4.      Set properties to javascript rule as


Default : True
FunctionName: HideButtonBasedOnViewSelected
InvertResult: False
Library: select JavaScript library where HideButtonBasedOnViewSelected function code is.
Parameters: Need one parameter to HideButtonBasedOnViewSelected function,

Add Crm parameter, and set parameter value as SelectedControl



5.      Now save and upload changes to CRM, and publish changes.
6.      JavaScript code required to hide button is
for CRM 2013
function HideButtonBasedOnViewSelected (selectedCtrl) {
      
        var view = selectedCtrl.get_$1X_3();
        var query = view.selectedViewName;

        if (query == 'Your View Name') {
            return false;
        }
        else
            return true;
    }
For CRM 2015
function HideButtonBasedOnViewSelected (selectedCtrl) {             
        var query = selectedCtrl.get_viewTitle();
        if (query == 'Your View Name') {
            return false;
        }
        else
            return true;
    }
To get selected view name need to use some unsupported JavaScript. 
"selectedCtrl.get_$1X_3();" or "selectedCtrl.get_viewTitle()"
This is giving all details about selected view and default view of this grid. 
Here I am hiding button for all views except view name is "Active Students - 2015"
For CRM 2013:
function HideButtonBasedOnViewSelected(selectedCtrl) {
        
        var view = selectedCtrl.get_$1X_3();
        var query = view.selectedViewName;

        if (query != 'Active Students - 2015') {
            return false;
        }
        else
            return true;
    }

For CRM 2015:
function HideButtonBasedOnViewSelected(selectedCtrl) {        
           var query = selectedCtrl.get_viewTitle();
        if (query != 'Active Students - 2015') {
            return false;
        }
        else
            return true;
    }

Enroll button is visible only for Active student -2015 view.


10 comments:

  1. Am on CRM 2015 online. Applied your code and got this error:

    object doesn't support property or method 'get_$1X_3

    How to overcome it?

    Tiong

    ReplyDelete
  2. As this part of code is unsupported JavaScript, Microsoft changed this method.. In CRM 2015 you will get view name like

    var query = selectedCtrl.get_viewTitle();

    Try using this.

    ReplyDelete
  3. Life is full of many challenges. Challenges that will make you or break you depending on how you handle it. Visit my site for more updates. God Bless to your site.

    n8fan.net

    www.n8fan.net

    ReplyDelete
  4. Nice information and most powerful and useful

    (Q)What about based on form.

    Reply me soon

    ReplyDelete
    Replies
    1. Hi Siva,
      you can use same technique for form. you can get current form name, and based on form name you can show/ hide button

      function HideButtonBasedOnForm() {
      var formDetails = Xrm.Page.ui.formSelector.getCurrentItem;
      var formLabel = formDetails.getLabel();

      if (formLabel != 'Active Students - 2015') {
      return false;
      }
      else
      return true;
      }

      for more details, please check
      https://msdn.microsoft.com/en-us/library/gg309560.aspx





      Delete
  5. Hi Mahadeo
    This article was very helpful, I'm working on CRM 2015 and it is valid. We are upgrading to D365 next month. Will the JavaScript code be still valid for D365?
    Thank you

    ReplyDelete
  6. Use thie code for Dynamics 365 for Customer Engagement Version 9.0+
    ribbon button configuration same the foregoing

    selectedCtrl.getCurrentView(): Xrm.LookupObject
    entityType: "savedquery"
    id: "{00000000-0000-0000-00AA-000010001001}"
    name: "999.exampleView"
    __proto__: Object

    good luck

    ReplyDelete
    Replies
    1. For D365 , i tried this code but not working.
      Can you give complete set of code as this code is not working

      selectedCtrl.getCurrentView(): Xrm.LookupObject
      entityType: "savedquery"
      id: "{00000000-0000-0000-00AA-000010001001}"
      name: "999.exampleView"
      __proto__: Object

      Delete
  7. THANK YOU FOR THE INFORMATION
    PLEASE VISIT US
    CRM Solutions



    ReplyDelete
  8. For Dynamics 365
    enableRule: function (selectedCtrl) {
    debugger;
    var idView = "{......}";
    var selectedView = selectedCtrl._viewSelector.getCurrentView();
    if (selectedView) {
    if (selectedView.id === idView)
    return true;
    else
    return false;
    } else {
    return false;
    }

    }

    ReplyDelete