Posted on 4. February 2012

Add button to Sub Grid - but only when on a specific parent entity

The Ribbon Workbench for CRM 2011 makes it easy to add a ribbon button to SubGrid ribbons. Your new custom button will be always shown when you have selected a subgrid on a related (1:N) entity parent entity form. Of course entities can have more than one relationship and so the same SubGrid ribbon is displayed from lots of different places. An example of this is the 'Account->Contact' and 'Contact->Sub-Contact' relationships. If you click on Contacts from an Account form, you will get the same Contact SubGrid ribbon as if you click on Sub-Contacts from the Contact Form. This may be fine, but what happens if you want to show different buttons on these two ribbons? Suppose you had a function that was only needed for Contact Sub-Contacts, but not for Account-Contacts? In this situation, the FormEntityContextRule is your friend.

The following tutorial show how to make a button visible only on the Contact->Sub-Contacts SubGrid, but not the Account->Contacts ribbon. It starts by first adding a button that appears on both, then an DisplayRule is added using the FormEntityContextRule to control the visibility.

 

The resulting Xml would be:

<RibbonDiffXml>
  <CustomActions>
    <CustomAction Id="demo.contact.Button1.Button.CustomAction" Location="Mscrm.SubGrid.contact.MainTab.Management.Controls._children" Sequence="60">
      <CommandUIDefinition>
        <Button Command="demo.contact.NewButton.Command" Id="demo.contact.Button1.Button" Image32by32="$webresource:new_Image32.png" Image16by16="$webresource:new_Image16.png" LabelText="$LocLabels:demo.contact.Button1.Button.LabelText" Sequence="60" TemplateAlias="o1" />
      </CommandUIDefinition>
    </CustomAction>
  </CustomActions>
  <Templates>
    <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
  </Templates>
  <CommandDefinitions>
    <CommandDefinition Id="demo.contact.NewButton.Command">
      <EnableRules />
      <DisplayRules>
        <DisplayRule Id="demo.contact.OnlyOnContactFormSubGrid.DisplayRule" />
      </DisplayRules>
      <Actions>
        <Url Address="about:blank" WinMode="1" />
      </Actions>
    </CommandDefinition>
  </CommandDefinitions>
  <RuleDefinitions>
    <TabDisplayRules />
    <DisplayRules>
      <DisplayRule Id="demo.contact.OnlyOnContactFormSubGrid.DisplayRule">
        <FormEntityContextRule EntityName="contact" />
      </DisplayRule>
    </DisplayRules>
    <EnableRules />
  </RuleDefinitions>
  <LocLabels>
    <LocLabel Id="demo.contact.Button1.Button.LabelText">
      <Titles>
        <Title description="SubGrid Button" languagecode="1033" />
      </Titles>
    </LocLabel>
  </LocLabels>
</RibbonDiffXml>

 

If course, you could use an OrRule to add multiple conditions to make it appear on multiple parent forms, but not all of them. This would change your rule to be:

<DisplayRule Id="demo.contact.OnlyOnContactFormSubGrid.DisplayRule">
    <OrRule>
       <Or>
          <FormEntityContextRule EntityName="contact" />
       </Or>
       <Or>
          <FormEntityContextRule EntityName="account" />
       </Or>
    </OrRule>
</DisplayRule>

I hope you agree that using the Ribbon Workbench can shave hours off of your development time when customising the CRM2011 Ribbon!

The Ribbon Workbench for CRM 2011 is a free download.

If you find an issue with the Ribbon Workbench for Dynamics CRM 2011 or have any suggestions, please send it here

Happy Ribbon Customising!

Comments are closed