Please leave feedback and suggestions.
Thanks
function HideExisting( PrimaryEntityTypeCode)
{
if (PrimaryEntityTypeCode ==1)
{
return true;
}
else
{
return false;
}
}
<RibbonDiffXml>
<CustomActions />
<Templates>
<RibbonTemplates Id=“Mscrm.Templates“></RibbonTemplates>
</Templates>
<CommandDefinitions />
<RuleDefinitions>
<TabDisplayRules />
<DisplayRules />
<EnableRules />
</RuleDefinitions>
<LocLabels />
</RibbonDiffXml>
<CustomActions>
<CustomAction Id="AddExistingCustomAction" Location="Mscrm.SubGrid.contact.AddExistingAssoc" Sequence="40">
<CommandUIDefinition>
<Button Id="Mscrm.SubGrid.contact.AddExistingAssoc" Command="Mscrm.AddExistingRecordFromSubGridAssociated" Sequence="40" LabelText="$Resources(EntityDisplayName):Ribbon.SubGrid.AddExisting" Alt="$Resources(EntityDisplayName):Ribbon.SubGrid.AddExisting" Image16by16="/_imgs/ribbon/AddExistingStandard_16.png" Image32by32="/_imgs/ribbon/AddExistingStandard_32.png" TemplateAlias="o1" ToolTipTitle="$Resources(EntityDisplayName):Mscrm_SubGrid_EntityLogicalName_MainTab_Management_AddExistingAssoc_ToolTipTitle" ToolTipDescription="$Resources(EntityDisplayName):Mscrm_SubGrid_EntityLogicalName_MainTab_Management_AddExistingAssoc_ToolTipDescription" />
</CommandUIDefinition>
</CustomAction>
<CustomAction Id="AddExistingCustomAction2" Location="Mscrm.SubGrid.contact.AddExistingStandard" Sequence="40">
<CommandUIDefinition>
<Button Id="Mscrm.SubGrid.contact.AddExistingStandard" Command="Mscrm.AddExistingRecordFromSubGridStandard" Sequence="30" LabelText="$Resources(EntityDisplayName):Ribbon.SubGrid.AddExisting" Alt="$Resources(EntityDisplayName):Ribbon.SubGrid.AddExisting" Image16by16="/_imgs/ribbon/AddExistingStandard_16.png" Image32by32="/_imgs/ribbon/AddExistingStandard_32.png" TemplateAlias="o1" ToolTipTitle="$Resources(EntityDisplayName):Mscrm_SubGrid_EntityLogicalName_MainTab_Management_AddExistingStandard_ToolTipTitle" ToolTipDescription="$Resources(EntityDisplayName):Mscrm_SubGrid_EntityLogicalName_MainTab_Management_AddExistingStandard_ToolTipDescription" />
</CommandUIDefinition>
</CustomAction>
</CustomActions>
We need a <CustomAction> to change the behaviour of the button. Make sure location property of <CustomAction> tag is exactly the same as Id property of <Button> tag.
<CommandDefinitions>
<CommandDefinition Id="Mscrm.AddExistingRecordFromSubGridStandard">
<EnableRules>
<EnableRule Id="Mscrm.AppendToPrimary" />
<EnableRule Id="Mscrm.EntityFormIsEnabled" />
<EnableRule Id="Mscrm.AddExistingCustomRule" />
</EnableRules>
<DisplayRules>
<DisplayRule Id="Mscrm.ShowForOneToManyGrids" />
<DisplayRule Id="Mscrm.AppendToPrimary" />
<DisplayRule Id="Mscrm.AppendSelected" />
<DisplayRule Id="Mscrm.CanWriteSelected" />
</DisplayRules>
<Actions>
<JavaScriptFunction FunctionName="Mscrm.GridRibbonActions.addExistingFromSubGridStandard" Library="/_static/_common/scripts/RibbonActions.js">
<CrmParameter Value="SelectedEntityTypeCode" />
<CrmParameter Value="SelectedControl" />
</JavaScriptFunction>
</Actions>
</CommandDefinition>
<CommandDefinition Id="Mscrm.AddExistingRecordFromSubGridAssociated">
<EnableRules>
<EnableRule Id="Mscrm.AppendPrimary" />
<EnableRule Id="Mscrm.AppendToPrimary" />
<EnableRule Id="Mscrm.EntityFormIsEnabled" />
<EnableRule Id="Mscrm.AddExistingCustomRule" />
</EnableRules>
<DisplayRules>
<DisplayRule Id="Mscrm.ShowForManyToManyGrids" />
<DisplayRule Id="Mscrm.AppendPrimary" />
<DisplayRule Id="Mscrm.AppendToPrimary" />
<DisplayRule Id="Mscrm.AppendSelected" />
<DisplayRule Id="Mscrm.AppendToSelected" />
<DisplayRule Id="Mscrm.CanWriteSelected" />
</DisplayRules>
<Actions>
<JavaScriptFunction FunctionName="Mscrm.GridRibbonActions.addExistingFromSubGridAssociated" Library="/_static/_common/scripts/RibbonActions.js">
<CrmParameter Value="SelectedEntityTypeCode" />
<CrmParameter Value="SelectedControl" />
</JavaScriptFunction>
</Actions>
</CommandDefinition>
</CommandDefinitions>
<EnableRules>
<EnableRule Id="Mscrm.AddExistingCustomRule">
<CustomRule FunctionName="HideExisting" Library="$webresource:new_CustomRule.js" Default="false" >
<CrmParameter Value="PrimaryEntityTypeCode" />
</CustomRule>
</EnableRule>
</EnableRules>
In the <CustumRole> I am calling a function HideExisting from webresource CustomRule.js mentioned in step 1. The default return value for this function false.The <CustomRule> is also passing PrimaryEntityTypeCode as parameter to HideExisting function If the function return true, the system will enable the “Add Existing Contact” button otherwise it will be disable the button.