dynamics-crmribbondynamics-crm-2016crm-ribbon-workbench

How to hide a vanilla button according to form state


I am trying to hide my SAVE vanilla button according to form state. when the form state != create the vanilla button should not display. I tried different things but nothing works:

  1. I create a function in js that returns true if form is create state

    function isHideState(){
    formstate = Xrm.Page.ui.getFormType();
    if(formstate == formType.create){
    return true;}
    else{
    return false;}
    }
    
  2. I added a disply rule and connected it to my command that relevant to the js function : my rule is : FormStateRule and state: Create

  3. I connected my command to my vanilla button and yet it display even if the form is'nt in create state.

Am I missing something? it's been hours.. guidance someone?

UPDATE: to be more specific - I need the button to be seen only on create mode.


Solution

  • Note: Whenever you are customizing the vanilla button (OOB Save button in your case), Make sure to start by right click the button in Ribbon workbench & click customize button/command to “retain” the OOB behavior & add your customizations on top of it.

    Change this line

    if(formstate = formType.create){
    

    into

    if(formstate == formType.create){
    

    Single = is for assignment; double = is for comparison.

    Update:

    RibbonDiffXml follows/expects this structure in command:

    <CommandDefinition
    Id="String">
     <EnableRules />
     <DisplayRules />
     <Actions />
    </CommandDefinition>
    

    There’s no direct property for rules in Button; only command can be linked.

    <Button Alt="String"
      Command="String"
      CommandType=["General" | "OptionSelection" | "IgnoredByMenu" ]
      CommandValueId="String"
      Description="String"
      Id="String"
      Image16by16="String"
      Image16by16Class="String"
      Image16by16Left="Non Positive Integer"
      Image16by16Top="Non Positive Integer"
      Image32by32="String"
      Image32by32Class="String"
      Image32by32Left="String"
      Image32by32Top="String"
      LabelCss="String"
      LabelText="String"
      MenuItemId="String"
      ModernCommandType=[ "ControlCommand"| "System"]
      ModernImage=”String”
      Sequence="1"
      TemplateAlias="String"
      ToolTipDescription="String"
      ToolTipHelpKeyWord="String"
      ToolTipImage32by32="String"
      ToolTipImage32by32Class="String"
      ToolTipImage32by32Left="Non Positive Integer"
      ToolTipImage32by32Top="Non Positive Integer"
      ToolTipShortcutKey="String"
      ToolTipTitle="String"
    />
    

    After 2013, commandbar introduction changed the behavior of Enable rule similar to Display rule. Disabled buttons using Enable rule will hide the button to utilize the space for other buttons in command bar (as there are always limitation like 7 or 9 buttons in command bar unlike Ribbon).

    Enabling buttons again will act like show/hide once switched (similar to Display rule). Probably you can follow this blogpost to achieve yours.

    An important thing to remember is to add the enable rule(s) to the command. This is commonly missed, someone creates an enable rule but forgets to add it to the button command.

    If you forget to add enable rules to the command then the button will show on all states/stages of the form. If you forget to add the command to the button, then the button will not show up on the form.