Thursday, December 20, 2012

Add a ribbon button in CRM 2011


   The followings are the steps to be followed to add a ribbon button in CRM 2011.
1.Create a solution in CRM 2011.
2.Add button to an entity ribbon.

1.Create a solution in CRM 2011

  • Create new solution in CRM 2011(Settings à Customization à Solution à New)
  • Open created solution and add desired entity(Account in this example) and avoid adding related components. Save and close the solution.
  •   Select the solution and export it as Unmanaged.
  • Extract the zip file and open customizations.xml in Visual Studio.


2.Add button to an entity ribbon

  • In opened customizations.xml file, search for <Ribbondiff> tag.You can see the below block of tags if there is no ribbon customization for this entity.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<RibbonDiffXml>
  <CustomActions />
  <Templates>
    <RibbonTemplates Id="Mscrm.Templates">
    </RibbonTemplates>
  </Templates>
  <CommandDefinitions />
  <RuleDefinitions>
    <TabDisplayRules />
    <DisplayRules />
    <EnableRules />
  </RuleDefinitions>
  <LocLabels />
</RibbonDiffXml>

  • In this example, we are adding a 'Close' button to an Account record ribbon to close the form.
  • We must have created 16X16 and 32X 32 Image webresources for Close button.
  • Replace <CustomActions /> tag with below XML tags.

1
2
3
4
5
6
7
<CustomActions>
  <CustomAction Id="Account.CustomAction.Close" Location="Mscrm.Form.account.MainTab.ExportData.Controls._children">
    <CommandUIDefinition>
      <Button Id="Account.form.Button.Close" Command="Account.form.Button.Close.Command" Sequence="34"
              LabelText="Close" ToolTipTitle="Close" ToolTipDescription="Close the form." TemplateAlias="o1" 
              Image16by16="$webresource:new_/Images/CloseButton16X16.png" Image32by32="$webresource:new_/Images/CloseButton32X32.png" />
    </CommandUIDefinition>
  </CustomAction>
</CustomActions>

  • Now we have to add command definition for Close button. CommandDefinition tag contains DisplayRules,  EnableRules and Actions for the button. Actions describes the action to be performed on click of button. And we must have created JavaScript web resource which has the function 'closeForm' to close the form.
  • Replace <CommandDefinitions /> tag with below XML tags.
  • Notice that, CommandDefinition Id must be same as Command in CommandUIDefinition in the above XML.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<CommandDefinitions>
  <CommandDefinition Id="Account.form.Button.Close.Command">
    <EnableRules>
      <EnableRule Id="Account.form.FormStateNotNew.EnableRule"/>
      <EnableRule Id="Account.form.Field.EnableRule"/>
    </EnableRules>
    <DisplayRules>
      <DisplayRule Id="Account.form.WebClient.DisplayRule"/>
    </DisplayRules>
    <Actions>
      <JavaScriptFunction Library="$webresource:new_/Scripts/Account.FormClose.js" FunctionName="closeForm"/>
    </Actions>
  </CommandDefinition>
</CommandDefinitions>

  • Replace <DisplayRules /> in <RuleDefinitions> tag with below tags to define display rules when this button should be visible.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<DisplayRules>
  <DisplayRule Id="Account.form.WebClient.DisplayRule"/>
    <OrRule>
      <Or>
        <CrmClientTypeRule Type="Web" />
      </Or>
      <Or>
        <CrmClientTypeRule Type="Outlook" />
      </Or>
    </OrRule>
  </DisplayRule>
</DisplayRules>

  • Replace <EnableRules/> in <RuleDefinitions> tag with below tags to define enable rules when this button should be enabled.


1
2
3
4
5
6
7
8
<EnableRules>
  <EnableRule  Id="Account.form.FormStateNotNew.EnableRule">
    <FormStateRule State="Create" InvertResult=="true" />
  </EnableRule>
  <EnableRule Id="Account.form.Field.EnableRule">
    <ValueRule Field="new_formcloseflag" Value=="true" />
  </EnableRule>
</EnableRules>


  • Save this XML file, Zip it, Import the solution and publish the entity.



5 comments:

  1. Really a very good explaination Thanx :), this was very helpful to me after visiting many site.

    ReplyDelete
    Replies
    1. Thank you for your feedback which let me write more.. :)

      Delete
  2. Hi,

    My system jobs are not running its always in 'waiting' status, all the workflows are in 'waiting'.
    Even Asyn Service is running I have checked the database also table 'AsyncSdkRootDomain', 'ADSdkRootDomain' , and 'ADWebApplicationRootDomain' are having the same value not update is required not suggest what to do... it would be very great for me if u can assist me in it... I wrote you bcz ur ribbon blog was good.

    ReplyDelete
    Replies
    1. Try restarting Asynchronous service first.
      If doesn't works, open one of the Workflow which is in 'Waiting' status. Then check the log in that workflow if any errors in any steps.

      Delete
    2. While chking the log I found that System Job shows error in CRM with : Code 2147204346, now can you suggest me what do do next as I have chk the Asyn

      Delete