user1160771
user1160771

Reputation: 395

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!

Upvotes: 1

Views: 5227

Answers (3)

Bojan Borisovski
Bojan Borisovski

Reputation: 852

This is how i hide the delete button on invoice form, based on status code, onload of the form:

    var statusVal = Xrm.Page.getAttribute("statuscode").getValue();
    if (statusVal==4 || statusVal==6) {
    //Disable delete button
    var x =top.document.getElementById("invoice|NoRelationship|Form|Mscrm.Form.invoice.Delete-Medium");
    x.style.display='none';  

You can get the id of the element span, which you want to hide from the source of the page.

Upvotes: 0

Peter Majeed
Peter Majeed

Reputation: 5352

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.

Upvotes: 3

Anwar
Anwar

Reputation: 4508

This article explains exactly what your are looking for.

Please follow this link How To Use "ValueRule" and "OrRule" in Ribbon Customizations - CRM 2011

Upvotes: 1

Related Questions