Archive for the ‘Ribbons’ Category

In one of my requirement i need to update the selected records in the home page contact entity grid by clicking on the custom ribbon button. Below is the code to add a ribbon button in the home page contact entity and update the selected records.

To update the records using OData, you need to add jquery1.4.1.min and json2 files. You can get those files from sdk
path – \sdk\samplecode\js\restendpoint\jqueryrestdataoperations\jqueryrestdataoperations\scripts.

Create a new solution, add the contact entity and export the solution. Open the customizations.xml file and replace the <RibbonDiffXml> with following code.

<RibbonDiffXml>
        <CustomActions>
          <CustomAction Id="Isv.contact.MainTab.CustomAction" Location="Mscrm.HomepageGrid.contact.MainTab.Collaborate.Controls._children" Sequence="41">
            <CommandUIDefinition>
              <Button Id="Isv.Grid.Contact.MainTab" Command="Isv.Grid.Contact.UpdateRecord.Command" LabelText="$LocLabels:Isv.Grid.Contact.UpdateRecord.LabelText" ToolTipTitle="$LocLabels:Isv.Grid.Contact.UpdateRecord.ToolTip" ToolTipDescription="$LocLabels:Isv.Grid.Contact.UpdateRecord.ToolTip" TemplateAlias="o1" Image16by16="/_imgs/ribbon/SendShortcut_16.png" Image32by32="/_imgs/ribbon/SendShortcut_32.png" />
            </CommandUIDefinition>
          </CustomAction>
        </CustomActions>
        <Templates>
          <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
        </Templates>
        <CommandDefinitions>
          <CommandDefinition Id="Isv.Grid.Contact.UpdateRecord.Command">
            <EnableRules>
              <EnableRule Id="Isv.Grid.Contact.OneSelected.EnableRule" />
            </EnableRules>
            <DisplayRules>
              <DisplayRule Id="Isv.Grid.Contact.UpdateRecord.DisplayRule" />
            </DisplayRules>
            <Actions>
              <JavaScriptFunction Library="$webresource:crm_json2.js" FunctionName="IsNAN">
              </JavaScriptFunction>
              <JavaScriptFunction Library="$webresource:crm_jquery1.4.1.min.js" FunctionName="IsNAN">
                </JavaScriptFunction>
              <JavaScriptFunction Library="$webresource:crm_contact.js" FunctionName="updateRecord">
                <CrmParameter Value="SelectedControlSelectedItemIds" />
              </JavaScriptFunction>
            </Actions>
          </CommandDefinition>
        </CommandDefinitions>
        <RuleDefinitions>
          <TabDisplayRules />
          <DisplayRules>
            <DisplayRule Id="Isv.Grid.Contact.UpdateRecord.DisplayRule">
              <CrmClientTypeRule Type="Web" />
            </DisplayRule>
          </DisplayRules>
          <EnableRules>
            <EnableRule Id="Isv.Grid.Contact.OneSelected.EnableRule">
              <SelectionCountRule AppliesTo="SelectedEntity" Minimum="1" />
            </EnableRule>
          </EnableRules>
        </RuleDefinitions>
        <LocLabels>
          <LocLabel Id="Isv.Grid.Contact.UpdateRecord.LabelText">
            <Titles>
              <Title languagecode="1033" description="Update Contacts" />
            </Titles>
          </LocLabel>
          <LocLabel Id="Isv.Grid.Contact.UpdateRecord.ToolTip">
            <Titles>
              <Title languagecode="1033" description="Update the selected Contact records" />
            </Titles>
          </LocLabel>
        </LocLabels>
      </RibbonDiffXml>

After replacing the code save the file and import the solution into the CRM.

Make sure the jscript file and function name exists in your webresource. Below is my jscript function

function updateRecord(selectedIds) {
    if (selectedIds != null && selectedIds != "") {
        var strIds = selectedIds.toString();
        var arrIds = strIds.split(",");
        for (var indxIds = 0; indxIds < arrIds.length; indxIds++) {
            updateContactRecords(arrIds[indxIds]);
        }
        alert("Selected Records Updated Successfully");
    }
    else {
        alert("No records selected!");
    }
}

function updateContactRecords(contactId) {
    var objContact = new Object();
    objContact.CreditOnHold = true;

    var jsonEntity = window.JSON.stringify(objContact);

    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc/ContactSet";
    var ODataPath = serverUrl + ODATA_ENDPOINT;
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: ODataPath + "(guid'" + contactId + "')",
        data: jsonEntity,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
            XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
            alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
        }
    });
}

Click here to view all the <CrmParameter> values that can be passed.

Adding a Custom Tab and Group to SubGrid in CRM 2011

Here i need to add a custom button by creating a new custom tab to the subgrid.

In my case I’m going to add sub grid button for opportunity entity. Create a new solution, add the opportunity entity and export the solution.

Open the customizations.xml file and replace the <RibbonDiffXml> with following code

<RibbonDiffXml>
        <CustomActions>
          <CustomAction Id="Mscrm.SubGrid.opportunity.CustomTab.CustomAction" Location="Mscrm.SubGrid.opportunity.ContextualTabs._children" Sequence="40">
            <CommandUIDefinition>
              <Tab Id="Mscrm.SubGrid.opportunity.CustomTab" Command="Mscrm.SubGrid.opportunity.CustomTab" Title="$LocLabels:Mscrm.SubGrid.opportunity.CustomTab.Title" Description="$LocLabels:Mscrm.SubGrid.opportunity.CustomTab.Title" Sequence="500">
                <Scaling Id="Mscrm.SubGrid.opportunity.CustomTab.Scaling">
                  <MaxSize Id="Mscrm.SubGrid.opportunity.CustomTab.CustomGroup.MaxSize" GroupId="Mscrm.SubGrid.opportunity.CustomTab.CustomGroup" Sequence="10" Size="LargeMedium" />
                </Scaling>
                <Groups Id="Mscrm.SubGrid.opportunity.CustomTab.Groups">
                  <Group Id="Mscrm.SubGrid.opportunity.CustomTab.CustomGroup" Command="Mscrm.SubGrid.opportunity.CustomGroup" Sequence="10" Title="$LocLabels:Mscrm.SubGrid.opportunity.CustomGroup.Title" Template="Mscrm.Templates.3.3">
                    <Controls Id="Mscrm.SubGrid.opportunity.CustomTab.CustomGroup.Controls">
                      <Button Id="Mscrm.SubGrid.opportunity.CustomTab.CustomGroup.CustomButton" ToolTipTitle="$LocLabels:Mscrm.SubGrid.opportunity.CustomButton.ToolTip" ToolTipDescription="$LocLabels:Mscrm.SubGrid.opportunity.CustomButton.ToolTip" Command="Mscrm.SubGrid.opportunity.CustomButton" Sequence="10" LabelText="$LocLabels:Mscrm.SubGrid.opportunity.CustomButton.LabelText" Image16by16="/_imgs/Ribbon/Enable_16.png" Image32by32="/_imgs/Ribbon/Enable_32.png" TemplateAlias="o1" />
                    </Controls>
                  </Group>
                </Groups>
              </Tab>
            </CommandUIDefinition>
          </CustomAction>
        </CustomActions>
        <Templates>
          <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
        </Templates>
        <CommandDefinitions>
          <CommandDefinition Id="Mscrm.SubGrid.opportunity.CustomButton">
            <EnableRules>
              <EnableRule Id="Mscrm.Enabled" />
            </EnableRules>
            <DisplayRules/>
            <Actions>
              <JavaScriptFunction Library="$webresource:crm_accountjscript" FunctionName="RibbonButtonClick" />
            </Actions>
          </CommandDefinition>
          <CommandDefinition Id="Mscrm.SubGrid.opportunity.CustomTab">
            <EnableRules>
              <EnableRule Id="Mscrm.Enabled" />
            </EnableRules>
            <DisplayRules>
              <DisplayRule Id="Mscrm.SubGrid.opportunity.CustomTab.DisplayRule" />
            </DisplayRules>
            <Actions />
          </CommandDefinition>
          <CommandDefinition Id="Mscrm.SubGrid.opportunity.CustomGroup">
            <EnableRules>
              <EnableRule Id="Mscrm.Enabled" />
            </EnableRules>
            <DisplayRules>
              <DisplayRule Id="Mscrm.SubGrid.opportunity.CustomGroup.DisplayRule" />
            </DisplayRules>
            <Actions />
          </CommandDefinition>
        </CommandDefinitions>
        <RuleDefinitions>
          <TabDisplayRules>
            <TabDisplayRule TabCommand="Mscrm.SubGrid.opportunity.CustomTab">
              <EntityRule EntityName="opportunity" Context="SubGridStandard" />
              <EntityRule EntityName="opportunity" Context="SubGridAssociated" />
            </TabDisplayRule>
          </TabDisplayRules>
          <DisplayRules>
            <DisplayRule Id="Mscrm.SubGrid.opportunity.CustomTab.DisplayRule">
              <CrmClientTypeRule Type="Web" />
            </DisplayRule>
            <DisplayRule Id="Mscrm.SubGrid.opportunity.CustomGroup.DisplayRule">
              <CrmClientTypeRule Type="Web" />
            </DisplayRule>
          </DisplayRules>
          <EnableRules>
            <EnableRule Id="Mscrm.Enabled">
              <CrmClientTypeRule Type="Web" />
            </EnableRule>
          </EnableRules>
        </RuleDefinitions>
        <LocLabels>
          <LocLabel Id="Mscrm.SubGrid.opportunity.CustomButton.LabelText">
            <Titles>
              <Title languagecode="1033" description="Custom Button" />
            </Titles>
          </LocLabel>
          <LocLabel Id="Mscrm.SubGrid.opportunity.CustomButton.ToolTip">
            <Titles>
              <Title languagecode="1033" description="Custom Button" />
            </Titles>
          </LocLabel>
          <LocLabel Id="Mscrm.SubGrid.opportunity.CustomGroup.Title">
            <Titles>
              <Title languagecode="1033" description="Custom Group" />
            </Titles>
          </LocLabel>
          <LocLabel Id="Mscrm.SubGrid.opportunity.CustomTab.Title">
            <Titles>
              <Title languagecode="1033" description="Custom Tab" />
            </Titles>
          </LocLabel>
        </LocLabels>
      </RibbonDiffXml>

After replacing the code save the file and import the solution into the CRM.

Make sure the jscript file and function name exists in your webresource. Below is my jscript function

function RibbonButtonClick() {
    alert("Ribbon click fired");
}

Adding a Ribbon Button in Custom Group to SubGrid in CRM 2011

<RibbonDiffXml>
        <CustomActions>
          <CustomAction Id="Mscrm.Isv.opportunity.subgrid.MaxSizeCustomAction" Location="Mscrm.SubGrid.opportunity.MainTab.Scaling._children" Sequence="110">
            <CommandUIDefinition>
              <MaxSize Id="Mscrm.Isv.opportunity.subgrid.MaxSize" GroupId="Mscrm.Isv.opportunity.subgrid.Group0" Sequence="21" Size="LargeLarge" />
            </CommandUIDefinition>
          </CustomAction>
          <CustomAction Id="Mscrm.Isv.opportunity.subgrid.ScaleSizeCustomAction" Location="Mscrm.SubGrid.opportunity.MainTab.Scaling._children" Sequence="130">
            <CommandUIDefinition>
              <Scale Id="Mscrm.Isv.opportunity.subgrid.Popup.1" GroupId="Mscrm.Isv.opportunity.subgrid.Group0" Sequence="85" Size="Popup" />
            </CommandUIDefinition>
          </CustomAction>
          <CustomAction Id="Mscrm.Isv.opportunity.subgrid.SubgridCustomAction" Location="Mscrm.SubGrid.opportunity.MainTab.Groups._children" Sequence="300">
            <CommandUIDefinition>
              <Group Id="Mscrm.Isv.opportunity.subgrid.Group0" Sequence="600" Command="Mscrm.Isv.opportunity.subgrid.Group0" Title="$LocLabels:Mscrm.Isv.opportunity.CustomGroup.LabelText" Template="Mscrm.Templates.Flexible2">
                <Controls Id="Mscrm.Isv.opportunity.subgrid.Controls">
                  <Button Id="Mscrm.Isv.opportunity.form.CustomButton.Button"
                              Command="Mscrm.Isv.opportunity.form.CustomButton.Command"
                              LabelText="$LocLabels:Mscrm.Isv.opportunity.CustomButton.LabelText"
                              ToolTipTitle="$LocLabels:Mscrm.Isv.opportunity.CustomButton.LabelText"
                              ToolTipDescription="$LocLabels:Mscrm.Isv.opportunity.CustomButton.ToolTip"
                              TemplateAlias="o1"
                              Image16by16="/_imgs/Ribbon/Enable_16.png"
                              Image32by32="/_imgs/Ribbon/Enable_32.png" />
                </Controls>
              </Group>
            </CommandUIDefinition>
          </CustomAction>
        </CustomActions>
        <Templates>
          <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
        </Templates>
        <CommandDefinitions>
          <CommandDefinition Id="Mscrm.Isv.opportunity.subgrid.Command">
            <EnableRules />
            <DisplayRules>
              <DisplayRule Id="Mscrm.Isv.opportunity.subgrid.Notify.DisplayRule" />
            </DisplayRules>
            <Actions />
          </CommandDefinition>
          <CommandDefinition Id="Mscrm.Isv.opportunity.subgrid.Group0">
            <EnableRules />
            <DisplayRules>
              <DisplayRule Id="Mscrm.Isv.opportunity.subgrid.Notify.DisplayRule" />
            </DisplayRules>
            <Actions />
          </CommandDefinition>
          <CommandDefinition Id="Mscrm.Isv.opportunity.form.CustomButton.Command">
            <EnableRules />
            <DisplayRules />
            <Actions>
              <JavaScriptFunction Library="$webresource:crm_accountjscript" FunctionName="RibbonButtonClick" />
            </Actions>
          </CommandDefinition>
        </CommandDefinitions>
        <RuleDefinitions>
          <TabDisplayRules />
          <DisplayRules>
            <DisplayRule Id="Mscrm.Isv.opportunity.subgrid.Notify.DisplayRule">
              <CrmClientTypeRule Type="Web" />
            </DisplayRule>
          </DisplayRules>
          <EnableRules />
        </RuleDefinitions>
        <LocLabels>
          <LocLabel Id="Mscrm.Isv.opportunity.CustomButton.ToolTip">
            <Titles>
              <Title languagecode="1033" description="Custom Button" />
            </Titles>
          </LocLabel>
          <LocLabel Id="Mscrm.Isv.opportunity.CustomButton.LabelText">
            <Titles>
              <Title languagecode="1033" description="Custom Button" />
            </Titles>
          </LocLabel>
          <LocLabel Id="Mscrm.Isv.opportunity.CustomGroup.LabelText">
            <Titles>
              <Title languagecode="1033" description="Custom Group" />
            </Titles>
          </LocLabel>
        </LocLabels>
      </RibbonDiffXml>

Click Here for the Post

Thanks