Search code examples
ribbondynamics-crm-2011

Disable System Ribbon in CRM 2011


I would like to conditionally disable a System Ribbon = AddNew in a CRM 2011 form with javascript. That means if the statusCode is X or Y disable the Ribbon, how could I do this?

I tryed to get the ID of the Ribbon in order to change the classname of the Ribbon to disabled, but I got Null because the Ribbons are loaded asychronously!


Solution

  • To expand on Anwar's answer, the key to getting this to work is to intercept and repurpose the functionality of the AddNew ribbon button, but once you do, there won't be any need for hacking the Ribbon DOM. Below are a few steps that you can take to get there.

    1) Create a solution with the Application Ribbon included.

    2) Find in the SDK the sample ribbon for the Application Ribbon or build it yourself using the solution included in the SDK.

    \sdk\resources\exportedribbonxml

    \sdk\samplecode\cs\client\ribbon\exportribbonxml\exportedribbonxml

    3) Find in the Application Ribbon template the control you're interested in, which I assume in this case is the AddNew button in entity subgrids. Repurpose this control in a CustomAction and make the location of the CustomAction the same name as the control you want to repurpose.

    <CustomAction Id="YourOrg.SubGrid.{!EntityLogicalName}.AddNewStandard" 
        Location="Mscrm.SubGrid.{!EntityLogicalName}.AddNewStandard">
      <CommandUIDefinition>
        <Button Id="Mscrm.SubGrid.{!EntityLogicalName}.AddNewStandard" 
            Command="Mscrm.AddNewRecordFromSubGridStandard" Sequence="20" 
            LabelText="$Resources(EntityDisplayName):Ribbon.SubGrid.AddNew" 
            Alt="$Resources(EntityDisplayName):Ribbon.SubGrid.AddNew" 
            Image16by16="/_imgs/ribbon/NewRecord_16.png"
            Image32by32="/_imgs/ribbon/newrecord32.png" TemplateAlias="o1" 
            ToolTipTitle="$Resources(EntityDisplayName):Mscrm_SubGrid_EntityLogicalName_MainTab_Management_AddNewStandard_ToolTipTitle" 
            ToolTipDescription="$Resources(EntityDisplayName):Mscrm_SubGrid_EntityLogicalName_MainTab_Management_AddNewStandard_ToolTipDescription" />
      </CommandUIDefinition>
    </CustomAction>
    

    4) Find in the Application Ribbon template the definition of the command of this button, and using the exact definition as the basis of a new CommandDefinition, add in your own custom rule (in this case, a new EnableRule).

    <CommandDefinition Id="Mscrm.AddNewRecordFromSubGridStandard">
      <EnableRules>
        <EnableRule Id="Mscrm.AppendToPrimary" />
        <EnableRule Id="Mscrm.EntityFormIsEnabled" />
        <EnableRule Id="YourOrg.DisableNewStuff" /> <!--your custom rule-->
      </EnableRules>
      <DisplayRules>
        <DisplayRule Id="Mscrm.ShowForOneToManyGrids" />
        <DisplayRule Id="Mscrm.AppendToPrimary" />
        <DisplayRule Id="Mscrm.CreateSelectedEntityPermission" />
        <DisplayRule Id="Mscrm.AppendSelected" />
        <DisplayRule Id="Mscrm.HideAddNewForChildEntities" />
      </DisplayRules>
      <Actions>
        <JavaScriptFunction FunctionName="Mscrm.GridRibbonActions.addNewFromSubGridStandard" 
              Library="/_static/_common/scripts/RibbonActions.js">
          <CrmParameter Value="SelectedEntityTypeCode" />
          <CrmParameter Value="PrimaryEntityTypeCode" />
          <CrmParameter Value="FirstPrimaryItemId" />
          <CrmParameter Value="PrimaryControl" />
        </JavaScriptFunction>
      </Actions>
    </CommandDefinition>
    

    5) Here is where Anwar's answer comes in: using both the OrRule and the ValueRule, define your EnableRule to check on the statuscodes of the entity. The linked demonstration relates to a new ribbon button, but the same rules work for repurposed ribbon controls as well.

    Publish your changes when you're done and your ribbon should be all set. Because I'm not sure how familiar you are with RibbonXml, I'll also mention that Microsoft has some comprehensive walkthroughs that are pretty helpful (after much time studying them!) in picking it up.