Pages

Tuesday, September 3, 2013

MS Dynamics CRM 2011 - Ribbon Drop Down Menu

This blog will guide you, how to create drop down menu from the ribbon. It contains one group or button ('Audit') ,2 drop down menu and their commands/actions.



 1) Create a solution contains Application Ribbons and Site Map.

2) Export and unzip the solution
3) Open the customizations.xml, add the below code
<ImportExportXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <RibbonDiffXml>    <CustomActions>
      <CustomAction Id="DNS.BasicHomeTab.Reports.CustomAction" Location="Mscrm.BasicHomeTab.Groups._children" Sequence="10">
        <CommandUIDefinition>

Button/Group Part
          <Group Id="DNS.BasicHomeTab.Reports" Command="Mscrm.Enabled" Template="Mscrm.Templates.Flexible2" Sequence="100"
                 Title="Reports" Description="Reports">
            <Controls Id="DNS.BasicHomeTab.Reports.Controls">
              <FlyoutAnchor Command="DNS.BasicHomeTab.Reports.Audit.Command" Id="DNS.BasicHomeTab.Reports.Audit"
                            Image16by16="/_imgs/ribbon/RunReport_16.png" Image32by32="/_imgs/ribbon/runreport32.png"
                            ToolTipTitle="Audit"
                            ToolTipDescription="Audit Report"
                            LabelText="Audit" Sequence="25" TemplateAlias="o1">

Menu Part
                <Menu Id="DNS.BasicHomeTab.Reports.Audit.Menu">
                  <MenuSection Id="DNS.BasicHomeTab.Reports.Audit.MenuSection" Sequence="10">
                    <Controls Id="DNS.BasicHomeTab.Reports.Audit.MenuSection.Controls">
                      <Button Id="DNS.BasicHomeTab.Reports.Audit.MenuSection.DNSUserReport" LabelText="Match Report - User"
                              Command="DNS.BasicHomeTab.Reports.Audit.MenuSection.DNSUserReport.Load" 
                              ToolTipTitle="User Audit Match Report" ToolTipDescription="The User Audit Match Report"  Sequence="20"/>
                      <Button Id="DNS.BasicHomeTab.Reports.Audit.MenuSection.DNSAdminReport" LabelText="Match Report - DNS Admin"
                              Command="DNS.BasicHomeTab.Reports.Audit.MenuSection.DNAdminReport.Load"
                              ToolTipTitle="The DNS Admin Audit Match Report" ToolTipDescription="The DNS Admin Audit Match Report"  Sequence="20"/>
                    </Controls>
                  </MenuSection>
                </Menu>
              </FlyoutAnchor>
              <!--<Button Id="DNS.BasicHomeTab.Reports.Audit" Command="DNS.BasicHomeTab.Reports.Audit.Command" Sequence="50" ToolTipTitle="$LocLabels:DNS.BasicHomeTab.Reports.Audit.LabelText" LabelText="$LocLabels:DNS.BasicHomeTab.Reports.Audit.LabelText" ToolTipDescription="$LocLabels:DNS.BasicHomeTab.Reports.Audit.Description" TemplateAlias="o1" Image16by16="/_imgs/ribbon/RunReport_16.png" Image32by32="/_imgs/ribbon/runreport32.png" />-->
            </Controls>
          </Group>
        </CommandUIDefinition>
      </CustomAction>
      <CustomAction Id="DNS.BasicHomeTab.Reports.MaxSize.CustomAction" Location="Mscrm.BasicHomeTab.Scaling._children" Sequence="10">
        <CommandUIDefinition>
          <MaxSize Id="DNS.BasicHomeTab.Reports.MaxSize" GroupId="DNS.BasicHomeTab.Reports" Sequence="10" Size="LargeMedium" />
        </CommandUIDefinition>
      </CustomAction>
      <!--<CustomAction Id="DNS.BasicHomeTab.Reports.Scale.Popup.CustomAction" Location="Mscrm.BasicHomeTab.Scaling._children" Sequence="10">
        <CommandUIDefinition>
          <Scale Id="DNS.BasicHomeTab.Reports.Scale.Popup" GroupId="DNS.BasicHomeTab.Reports
          " Sequence="300" Size="Popup" />
        </CommandUIDefinition>
      </CustomAction>-->
    </CustomActions>

Command Definition Part
    <CommandDefinitions>
          <CommandDefinition Id="DNS.BasicHomeTab.Reports.Audit.MenuSection.DNSUserReport.Load">
            <EnableRules />
            <DisplayRules>
              <DisplayRule Id="DNS.BasicHomeTab.Reports.Audit.DisplayRule" />
            </DisplayRules>
            <Actions>
              <Url Address="/crmreports/viewer/viewer.aspx?action=filter&amp;helpID=ReportAudit.rdl&amp;id=%7b11111111-1111-1111-1111-111111111111%7d" PassParams="true" WinParams="height=200, width=400,toolbar=no, menubar=no,location=no" />
              <!--<JavaScriptFunction Library="$webresource:DNS_Javascript/Audit.js" FunctionName="LoadReport">
                <StringParameter Value="1" />
              </JavaScriptFunction>-->
            </Actions>
          </CommandDefinition>
          <CommandDefinition Id="DNS.BasicHomeTab.Reports.Audit.Command">
            <EnableRules />
            <DisplayRules>
              <DisplayRule Id="DNS.BasicHomeTab.Reports.Audit.DisplayRule" />
            </DisplayRules>
            <Actions/>
          </CommandDefinition>
          <CommandDefinition Id="DNSDNS.DashboardTab.Favorite.AbsApp.Command">
            <EnableRules />
            <DisplayRules />
            <Actions>
              <JavaScriptFunction Library="/_common/global.ashx" FunctionName="openObj">
                <IntParameter Value="112" />
              </JavaScriptFunction>
            </Actions>
          </CommandDefinition>
    </CommandDefinitions>

Rule Definition Part
    <RuleDefinitions>
          <TabDisplayRules/>
          <DisplayRules>
            <DisplayRule Id="DNS.BasicHomeTab.Reports.Audit.MenuSection.DNSAdminReport.DisplayRule">
              <EntityPrivilegeRule EntityName="report" PrivilegeType="Create" PrivilegeDepth="Deep" />
            </DisplayRule>
          </DisplayRules>
          <EnableRules />
    </RuleDefinitions>
  </RibbonDiffXml>
</ImportExportXml>
4) Zip and export the file solution to the CRM


Populate Menu thorough JavaScript based on the Security Role (Dynamic Menu)


//ImportExportXml//RibbonDiffXml//CustomActions
<CustomAction Id="DNS.BasicHomeTab.Reports.CustomAction" Location="Mscrm.BasicHomeTab.Groups._children" Sequence="10">
<CommandUIDefinition>
  <Group Id="DNS.BasicHomeTab.Reports" Command="Mscrm.Enabled" Template="Mscrm.Templates.Flexible2" Sequence="100" Title="Reports" Description="Reports">
    <Controls Id="DNS.BasicHomeTab.Reports.Controls">
      <!-- TemplateAlias="isv"  is small image-->
      <FlyoutAnchor Id="DNS.Reports.Menu.FlyoutAnchor" Sequence="25" Command="Mscrm.Enabled"
           Image16by16="/_imgs/ribbon/RunReport_16.png" Image32by32="/_imgs/ribbon/runreport32.png"
           LabelText="Audit" Alt="Audit" PopulateDynamically="true"
           PopulateQueryCommand="DNS.Reports.Menu.Load.Command" TemplateAlias="o1" />
    </Controls>
  </Group>
</CommandUIDefinition>
</CustomAction>

//ImportExportXml//RibbonDiffXml//CustomActions//CommandDefinitions
<CommandDefinition Id="DNS.Reports.Menu.Load.Command">
    <EnableRules />
    <DisplayRules>
      <DisplayRule Id="DNS.BasicHomeTab.Reports.Audit.DisplayRule" />
    </DisplayRules>
    <Actions>
      <JavaScriptFunction FunctionName="ReportMenu" Library="$webresource:DNS_JavaScript/auditmenu.js">
        <CrmParameter Value="CommandProperties" />
      </JavaScriptFunction>
    </Actions>
    </CommandDefinition>
    <CommandDefinition Id="DNS.Reports.Menu.Action.Command">
    <EnableRules />
    <DisplayRules />
    <Actions>
      <JavaScriptFunction FunctionName="MenuAction" Library="$webresource:DNS_JavaScript/auditmenu.js">
        <CrmParameter Value="CommandProperties" />
      </JavaScriptFunction>
    </Actions>
</CommandDefinition>

//DNS_JavaScript/auditmenu.js
includeJavaScript("/WebResources/DNS_JavaScript/jquery1.4.1.min.js");
includeJavaScript("/WebResources/DNS_JavaScript/operations.js");
includeJavaScript("/WebResources/DNS_JavaScript/audit.js");

var SysAdmin = false;
var DNSUser = false;
var ECM = false;
try {
    SysAdmin = CheckUserRole("System Administrator");

    if (!SysAdmin) {
        DNSUser = CheckUserRole("DNS User");
        if (!DNSUser) {
            ECM = CheckUserRole("Content Manager");
        }
    }
} catch(er) {
}

function ReportMenu(CommandProperties) {

    var buttons = "";
    var seq = 20;
    if (SysAdmin || ECM) {
        buttons += GenerateButton("DNS.Reports.Menu.MatchReportClerk", "CM Report", "CM Report", "DNS.Reports.Menu.Action.Command", seq);
        seq += 10;
    }
    if (SysAdmin || DNSUser) {
        buttons += GenerateButton("DNS.Reports.Menu.MatchReportDNSUser", "User Report", "User Report", "DNS.Reports.Menu.Action.Command", seq);
        seq += 10;
    }
    if (SysAdmin || ECM || DNSUser) {
        buttons += GenerateButton("DNS.Reports.Menu.MatchReport", "Audit Report", "Audit Report", "DNS.Reports.Menu.Action.Command", seq);
        seq += 10;
    }

    var menu = "<Menu Id='DNS.Reports.Menu.Menu'>";
    menu += "   <MenuSection Id='DNS.Reports.Menu.MenuSection' Sequence='10'>";
    menu += "       <Controls Id='DNS.Reports.Menu.Controls'>";
    menu +=     buttons;
    menu += "       </Controls>";
    menu += "   </MenuSection>";
    menu += "</Menu>";
    CommandProperties.PopulationXML = menu;
}

function GenerateButton(id, lxt, alt , cmd, seq) {
    return "<Button Id='" + id  + "' LabelText='" +  lxt + "' Alt='" + alt + "' Command='" + cmd + "' Sequence='" + seq + "'  />";
}

function MenuAction(CommandProperties) {
    var controlId = CommandProperties.SourceControlId;
    if (controlId == "DNS.Reports.Menu.MatchReportClerk") {
        window.open('/crmreports/viewer/viewer.aspx?action=run&helpID=CRMCMAuditMatch.rdl&id={12345678-8B06-E311-B0EF-0050568C2EE0}', '_blank');
    } else if (controlId == "DNS.Reports.Menu.MatchReportDNSUser") {
        window.open('/crmreports/viewer/viewer.aspx?action=run&helpID=CRMUserAuditMatch.rdl&id={A02A6C7A-1234-5678-9012-0050568C2EE0}', '_blank');
    } else if (controlId == "DNS.Reports.Menu.MatchReport") {
        alert("Audit Report");
    }
}

function includeJavaScript(filename) {
    var head = document.getElementsByTagName('head')[0];

    var script = document.createElement('script');
    script.src = filename;
    script.type = 'text/javascript';

    head.appendChild(script)
}

Reference Tools 
http://www.develop1.net/public/page/Ribbon-Workbench-for-Dynamics-CRM-2011.aspx
http://msdn.microsoft.com/en-us/library/gg309639.aspx
http://msdn.microsoft.com/en-us/library/gg327947.aspx
http://msdn.microsoft.com/en-us/library/gg309420.aspx
http://crmvisualribbonedit.codeplex.com/

No comments:

Post a Comment