Saturday, March 17, 2012

CRM 2011 Ribbon: Disabling/Enabling elements based on form fields

If you need to enable disable a ribbon control based on the value within a form field, you need to use the “ValueRule

Gotcha: Don’t use the default value element of the “ValueRule” as that seems to interfere with the null value check.

Here is a very simple example:

1. First create the enable rule:

<EnableRules>
      <EnableRule Id="Mscrm.Isv.opportunity.Form.Actions.MyButton.EnableRule">

        <ValueRule Field="fieldNameOnOpportunityPage" Value="null"
        InvertResult="false" />
      </EnableRule>
    </EnableRules>

2. Next reference the enable rule via its ID in your command definition:

<CommandDefinition Id="Mscrm.Isv.opportunity.Form.Actions.MyCommand">
      <EnableRules>
        <EnableRule Id="Mscrm.Isv.opportunity.Form.Actions.CreateNewProposal.EnableRule" />
      </EnableRules>
      <DisplayRules/>
      <Actions/>
    </CommandDefinition>

3. And in your button, you reference the command definition, by its ID

<Button Id="Mscrm.Isv.opportunity.Form.Actions.MyButton"
            Command="Mscrm.Isv.opportunity.Form.Actions.MyCommand"
            LabelText="$LocLabels:Mscrm.Isv.opportunity.Form.Actions.MyCommand.LocLabel"
            ToolTipTitle="$LocLabels:Mscrm.Isv.opportunity.Form.Actions.MyCommand.LocLabel"
            ToolTipDescription="$LocLabels:Mscrm.Isv.opportunity.Form.Actions.MyCommand.ToolTip.LocLabel"
            Image16by16="/_imgs/ico_18_acct.gif"
            Image32by32="/_imgs/ico_18_acct.gif"
            Image16by16Class="ms-crm-Upgraded-Ribbon-Image16"
            Image32by32Class="ms-crm-Upgraded-Ribbon-Image32"
            Sequence="10" TemplateAlias="o1" />

4. Finally, add the LocLabels for your button and you are done:

<LocLabels>
    <LocLabel Id="Mscrm.Isv.opportunity.Form.Actions.MyCommand.LocLabel">
      <Titles>
        <Title languagecode="1033"
        description="Create NCM Proposal" />
      </Titles>
    </LocLabel>
    <LocLabel Id="Mscrm.Isv.opportunity.Form.Actions.MyCommand.ToolTip.LocLabel">
      <Titles>
        <Title languagecode="1033"
        description="Create a New Proposal" />
      </Titles>
    </LocLabel>
</LocLabels>

 

Here is the complete example:

The demo also shows you how to create a custom group and add it to the “Main Tab” of the “Opportunity” entity. The button that I add “MyDemoButton” is added to the custom group “Demo Group”.

<RibbonDiffXml>
  <CustomActions>
    <!--Raj-->
    <CustomAction Id="Mscrm.Isv.opportunity.Form.Group.Demo.MaxSize.CustomAction"
    Location="Mscrm.Form.opportunity.MainTab.Scaling._children"
    Sequence="150">
      <CommandUIDefinition>
        <MaxSize Id="Mscrm.Isv.opportunity.Form.Group.Demo.MaxSize"
        GroupId="Mscrm.Isv.opportunity.Form.Group.Demo.Group"
        Sequence="21" Size="LargeLarge" />
      </CommandUIDefinition>
    </CustomAction>
    <CustomAction Id="Mscrm.Isv.opportunity.Form.Group.Demo.CustomAction"
    Location="Mscrm.Form.opportunity.MainTab.Groups._children"
    Sequence="115">
      <CommandUIDefinition>
        <Group Id="Mscrm.Isv.opportunity.Form.Group.Demo.Group"
        Command="Mscrm.Isv.opportunity.Form.Group.Demo.Command"
        Title="$LocLabels:Mscrm.Isv.opportunity.Form.Group.Demo.Title"
        Sequence="110" Template="Mscrm.Templates.Flexible2">
          <Controls Id="Mscrm.Isv.opportunity.Form.Group.Demo.Controls">
            <Button Id="Mscrm.Isv.opportunity.Form.Actions.MyDemoButton"
            Command="Mscrm.Isv.opportunity.Form.Actions.MyDemoButton"
            LabelText="$LocLabels:Mscrm.Isv.opportunity.Form.Actions.MyDemoButton.LocLabel"
            ToolTipTitle="$LocLabels:Mscrm.Isv.opportunity.Form.Actions.MyDemoButton.LocLabel"
            ToolTipDescription="$LocLabels:Mscrm.Isv.opportunity.Form.Actions.MyDemoButton.ToolTip.LocLabel"
            Image16by16="/_imgs/ico_18_acct.gif"
            Image32by32="/_imgs/ico_18_acct.gif"
            Image16by16Class="ms-crm-Upgraded-Ribbon-Image16"
            Image32by32Class="ms-crm-Upgraded-Ribbon-Image32"
            Sequence="10" TemplateAlias="o1" />
          </Controls>
        </Group>
      </CommandUIDefinition>
    </CustomAction>
  </CustomActions>
  <Templates>
    <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
  </Templates>
  <CommandDefinitions>
    <CommandDefinition Id="Mscrm.Isv.opportunity.Form.Group.Demo.Command">

      <EnableRules />
      <DisplayRules />
      <Actions />
    </CommandDefinition>
    <CommandDefinition Id="Mscrm.Isv.opportunity.Form.Actions.MyDemoButton">
      <EnableRules>
        <EnableRule Id="Mscrm.Isv.opportunity.Form.Actions.MyDemoButton.EnableRule" />
      </EnableRules>
      <DisplayRules>
        <DisplayRule Id="Mscrm.Isv.opportunity.Form.Actions.MyDemoButton" />
      </DisplayRules>
      <Actions>
        <Url Address="
http://www.google.com" />
      </Actions>
    </CommandDefinition>
  </CommandDefinitions>
  <RuleDefinitions>
    <TabDisplayRules></TabDisplayRules>
    <DisplayRules>
      <DisplayRule Id="Mscrm.Isv.opportunity.Form.Actions.MyDemoButton">
        <CrmOfflineAccessStateRule State="Offline"
        InvertResult="true" />
      </DisplayRule>
    </DisplayRules>
    <EnableRules>
      <EnableRule Id="Mscrm.Isv.opportunity.Form.Actions.MyDemoButton.EnableRule">
        <ValueRule Field="Demo_fieldOnFormId" Value="null"
        InvertResult="false" /> <!--replace demo_fieldOnFormId with an actual id-->
      </EnableRule>
    </EnableRules>
  </RuleDefinitions>
  <LocLabels>
    <LocLabel Id="Mscrm.Isv.opportunity.Form.Actions.MyDemoButton.LocLabel">
      <Titles>
        <Title languagecode="1033"
        description="This is a demo command" />
      </Titles>
    </LocLabel>
    <LocLabel Id="Mscrm.Isv.opportunity.Form.Actions.MyDemoButton.ToolTip.LocLabel">
      <Titles>
        <Title languagecode="1033"
        description="This is a demo command" />
      </Titles>
    </LocLabel>
    <LocLabel Id="Mscrm.Isv.opportunity.Form.Group.Demo.Title">
      <Titles>
        <Title languagecode="1033" description="Demo Group" />
      </Titles>
    </LocLabel>
  </LocLabels>
</RibbonDiffXml>

No comments: