Showing posts with label Ribbon. Show all posts
Showing posts with label Ribbon. Show all posts

Friday, June 15, 2012

Microsoft Dynamics CRM 2011 Create new case from account's homepage ribbon


Microsoft Dynamics CRM 2011 Create new case from account's homepage ribbon Or cut the clicks

This is a how to create new case from account grid using ribbon button, just like if you would create the case from case sub grid in account form. It gets all the attributes mapping from related account.
The idea behind it was to cut the time for creating new case, Instead of going to account homepage, opening the account form, click on case sub grid and then create the case, It will be just go to account homepage choose the account and create new case.
This is a real time saver.


1) Create new solution.
2) Add new web resource type JavaScript

var CASE_URL = "/main.aspx?etc=112&extraqs=%3f_CreateFromId%3d%257b{ACCOUNTID}%257d%26_CreateFromType%3d1&pagetype=entityrecord";
CreateNewCaseFromAccount = function(id)
{
                        //get accountid
                        var nakedId = id.replace("{", "").replace("}", "");
                        var caseUrl = CASE_URL.replace("{ACCOUNTID}", nakedId);
                        var caseUrl = Xrm.Page.context.prependOrgName(caseUrl);
                        window.open(caseUrl);
}
3) Add button to account's homepage ribbon to call CreateNewCaseFromAccount function. Two ways for adding the button to account.
1) Use Ribbon workbench.
2) The old fashion way, do it yourself.
a. Add account to solution.
b. Export solution.
c. Extract the solution file.
d. Open customizations.xml using your favorite notepad
e.Add the button to the RibbonDiffXml:

  <RibbonDiffXml>
        <CustomActions>
          <CustomAction Id="HomepageGrid.account.MainTab.Actions.CreateNewCase.CustomAction" Location="Mscrm.HomepageGrid.account.MainTab.Actions.Controls._children" Sequence="41">
            <CommandUIDefinition>
              <Button Id="HomepageGrid.account.MainTab.Actions.CreateNewCase" Command="HomepageGrid.account.MainTab.Actions.CreateNewCase.Command" Sequence="20" ToolTipTitle="Create New Case" LabelText="Create New Case" ToolTipDescription="Create New Case" TemplateAlias="o1" />
            </CommandUIDefinition>
          </CustomAction>
        </CustomActions>
        <Templates>
          <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
        </Templates>
        <CommandDefinitions>
          <CommandDefinition Id="HomepageGrid.account.MainTab.Actions.CreateNewCase.Command">
            <EnableRules>
              <EnableRule Id="HomepageGrid.account.MainTab.Actions.CreateNewCase.Command.EnableRule.SelectionCountRule" />
            </EnableRules>
            <DisplayRules />
            <Actions>
              <JavaScriptFunction FunctionName="CreateNewCaseFromAccount" Library="$Webresource:new_/CreateNewCase/CreateNewCase.js">
                <CrmParameter Value="FirstSelectedItemId" />
              </JavaScriptFunction>
            </Actions>
          </CommandDefinition>
        </CommandDefinitions>
        <RuleDefinitions>
          <TabDisplayRules />
          <DisplayRules />
          <EnableRules>
            <EnableRule Id="HomepageGrid.account.MainTab.Actions.CreateNewCase.Command.EnableRule.SelectionCountRule">
              <SelectionCountRule AppliesTo="SelectedEntity" Maximum="1" Minimum="1" Default="true" />
            </EnableRule>
          </EnableRules>
        </RuleDefinitions>
        <LocLabels />
      </RibbonDiffXml>

f. Import the solution back and publish.


Good luck.

This post was written before Dynamics CRM 2011 RU 8 and the new Xrm.utility, It changes only the way of opening new form the basic idea of this post stays the same.
For more information about Xrm.utility go to
http://jonasrapp.cinteros.se/2012/08/xrmutility-methods-in-ms-dynamics-crm.html
or many other posts that explains the new functions available.
 

Friday, September 16, 2011

MSCRM 2011 Ribbon dynamically display the save button.


Microsoft Dynamics CRM 2011 Ribbon is great once you get to know how to work with it. 
At the beginning it looked too complicated, there are too many tags to write and it is difficult to understand how to place the button in the right place. 
The good news are after understanding the why it works, it all makes sense.
They are very flexible and you can do all most everything in a supported why.

This blog shows how to remove and show Ribbon save buttons based on some logic.

In MSCRM 4 it can be done using unsupported code
Something like: 

var toolBarButtons = document.all.mnuBar1.rows[0].cells[0].childNodes[0].childNodes;
and then iterate over all the buttons till the save button found and removed / showed when needed.


In MSCRM 2011 there is supported and much more elegant solution:

Generally speaking you should know these steps before starting (Copied from: http://msdn.microsoft.com/en-us/library/gg334532.aspx):
1.     Prerequisites: This section describes the specific requirements and resources you need.
2.     Export: Create and export a solution containing just the solution components you need. For more information about creating and exporting a solution see the application Help and Export the Ribbon.
3.     Identify the IDs for existing items: Examine default ribbon definitions. The walkthrough provides the XML you need, but it is important that you understand the relationships to existing data in the default ribbon definitions.
4.     Create the script Web resource: Walkthroughs that add controls to the ribbon require a JScript Web resource to perform an action. For more information about creating JScript Web resources, see the application Help and JScript Libraries for Microsoft Dynamics CRM.
5.     Edit the customizations.xml file: This section leads you through a series of cumulative steps editing nodes within the <RibbonDiffXml> (RibbonDiffXml)element. The final step shows you the entire modified RibbonDiffXml element in the customizations.xml file.
6.     Import and publish the solution: Compress the modified solution files and import those files to update your solution. For more information about compressing and importing a solution, see the application Help and Import the Ribbon.
7.     Test and verify that the requirements are met: In this step you confirm that the requirements specified in the prerequisites section are met.


We'll twist it a little bit to meet our requirement.

1) Replace the CommandDefinition that descripes the save button with new one. We'll copy it from incidentribbon.xml and place it in the exported customization.xml.

      <CommandDefinition Id="Mscrm.SavePrimary">
        <EnableRules>
          <EnableRule Id="Mscrm.AvailableOnForm" />
          <EnableRule Id="Mscrm.CanSavePrimary" />
          <EnableRule Id="Mscrm.ReadPrimaryPermission" />
          <EnableRule Id="Mscrm.Form.incident.MainTab.CustomRule.EnableRule" />
        </EnableRules>
        <DisplayRules>
          <DisplayRule Id="Mscrm.CanSavePrimaryEntityType" />
        </DisplayRules>
        <Actions>
          <JavaScriptFunction FunctionName="Mscrm.RibbonActions.saveForm" Library="/_static/_common/scripts/RibbonActions.js">
            <CrmParameter Value="PrimaryControl" />
          </JavaScriptFunction>
        </Actions>
      </CommandDefinition>

2) add new Enable Rule which indicates should it be shown or not
<EnableRule Id="Mscrm.Form.incident.MainTab.CustomRule.EnableRule">
     <CustomRule FuncationName="EnableButtons" Library="$Webresource:new_incident_library" />
</EnableRule>

3) Now in your code you'll be able to decide would the save button be shown or not by defining the logic in the function EnableButtons. Whenever you want to change the display make sure the function returns true / false as expected and refresh the ribbon by using: 
Xrm.Page.ui.refreshRibbon()

The thing to remember here is that you can change the current setting of almost everything in the ribbon by redefining them.