Create a Dialog Short-Cut Ribbon Button

UPDATE: The code in this article has been updated to support CRM2015 Update 1. Technically it is not supported by the SDK but is the only way of reliably duplicating the out of the box dialog windows. If you need a fully supported approach you can use the technique described by Mahadeo Matre however this requires your customisations to be updated with the URL of each OnPrem deployment and does not always create a dialog window that is correctly sized.

Challenge:

In the How To 'Create a Workflow Short Cut Ribbon Button' we discussed that users often want to reduce the number of clicks it takes to perform common actions. Running Dialogs from Grids and Forms is no exception. Wouldn't it be nice to provide a 'short-cut' to a specific dialog rather than asking the user to search for it first - especially if this is a dialog that they use more than once a day.

Solution:

This solution involves creating a small javascript webresource that will be used by two ribbon buttons. One from the HomePage and Grid ribbons and one from Form ribbon. In this example, I am going to add the buttons to the contact entity.

  1. Open the Dialog that you want to provide the short cut to, and use the 'Copy Link' from the Actions menu. Paste the link into notepad and you will see something like:

    <http://<server>/<OrgName>/sfa/workflow/edit.aspx?id=%7b<Dialog ID>%7d>

  2. Extract the DialogID and save it for later.
    Note: If you move a dialog from one CRM organisation to another via a solution export/import, this ID will be the same on all instances. If you manually create the dialog on each instance, you'll need to update the ID for each instance.

  3. Create a solution and add the Contact Entity along with the ribbon button images that you want to use on your button. See the Getting Started Guide for more information on buttons.

  4. Add a new 'Web Resource' to your solution and set the type to JScript.

  5. Copy the following script into a text file, and then use the 'Upload' button on the web resource, selecting the text file you just created.
  6. // NOTE: This code is unsupported since it is not using the SDK
    // This is the only way of exactly mimicking the behaviour of the out of the box 
    // dialog windows that are reliably sized and positioned
    (function() {
    Develop1_RibbonCommands_runDialogGrid = function(ids, objectTypeCode, dialogId) {
        if ((ids == null) || (!ids.length)) {
            alert(window.LOCID_ACTION_NOITEMSELECTED);
            return;
        }
        if (ids.length > 1) {
            alert(window.LOCID_GRID_TOO_MANY_RECORDS_IWF);
            return;
        }
        var rundialog = Mscrm.CrmUri.create('/cs/dialog/rundialog.aspx');
        rundialog.get_query()['DialogId'] = dialogId;
        rundialog.get_query()['ObjectId'] = ids[0];
        rundialog.get_query()['EntityName'] = objectTypeCode;
        openStdWin(rundialog,buildWinName(null),615,480,null);
    }
    Develop1_RibbonCommands_runDialogForm = function(objectTypeCode, dialogId) {
        var primaryEntityId = Xrm.Page.data.entity.getId();
        var rundialog = Mscrm.CrmUri.create('/cs/dialog/rundialog.aspx');
        rundialog.get_query()['DialogId'] = dialogId;
        rundialog.get_query()['ObjectId'] = primaryEntityId;
        rundialog.get_query()['EntityName'] = objectTypeCode;
        var hostWindow = window;
            if (typeof(openStdWin) == 'undefined') {
                hostWindow = window.parent; // Support for Turbo-forms in CRM2015 Update 1
            }
            if (typeof(hostWindow.openStdWin) != 'undefined') {
                hostWindow.openStdWin(rundialog, hostWindow.buildWinName(null), 615, 480, null);
            } 
    }
    })();
    
  7. Give the webresource a name such as 'RunDialog.js' and then save and close.

  8. Load your new solution into the Ribbon Workbench. See the Getting Started Guide for more information on loading solutions.

  9. In the 'Solution Element' panel, add the following EnableRules and mark each one as 'IsCore=True' in the properties.

    -Mscrm.FormStateNotNew
    -Mscrm.SelectionCountExactlyOne
    -Mscrm.VisualizationPaneNotMaximized 

    IsCore Rules

  10. Under 'Commands' in the Solution Elements Panel, add two Commands and name them something like:
      -new.contact.RunDialogGrid.Command
      -new.contact.RunDialogForm.Command

  11. Right-Click on the Command 'new.contact.RunDialogForm.Command' and select 'Edit Enable Rules'
    Add the following Enabled Rule:
       -Mscrm.FormStateNotNew

    This will ensure that the button is only enabled if dialogs can be run on the current record.

  12. Right-Click on the Command 'new.contact.RunDialogGrid.Command' and select 'Edit Enable Rules'
    Add the following Enabled Rules:
    -Mscrm.SelectionCountExactlyOne
    -Mscrm.VisualizationPaneNotMaximized (Only add this rule if the entity you are adding the button to supports chart visualisations. If when you publish you get a message indicating that the that you command is dependant on this rule - remove it from your command and re-publish)

    This will ensure that the button will only be enabled if there is a single record selected and they can have dialogs run on them.

  13. This is where the magic happens! We create some commands to call the javascript.

    Righ-click on the Command 'new.contact.RunDialogGrid.Command' and select 'Edit Actions'
    Add a Javascript Command and set the following properties:

    Function Name Develop1_RibbonCommands_runDialogGrid
    Library Use the lookup button to select the webresource you created above
    Parameters 1: Type = Crm Parameter, Value = SelectedControlSelectedItemIds
    2: Type = Crm Parameter, Value = SelectedEntityTypeName
    3: Type = String, Value = Dialog ID found from the Copy Link in the previous steps

    runDialogGrid

  14. Righ-click on the Command 'new.contact.RunDialogForm.Command' and select 'Edit Actions'
    Add a Javascript Command and set the following properties:

    Function Name Develop1_RibbonCommands_runDialogForm
    Library Use the lookup button to select the webresource you created above
    Parameters 1: Type = Crm Parameter, Value = PrimaryEntityTypeName
    2: Type = String, Value = Dialog ID found from the Copy Link in the previous steps

    runDialogForm


    Adding the Buttons
  15. Select the 'Home Page' ribbon using the drop down in the top right of the design surface.
  16. Drag a new button on to the 'Process' Group (Or in CRM2013 or later anywhere on the home page command bar).

    Form

    Give your buttons an image, and select the Command 'new.contact.RunDialogGrid.Command'

  17. Select the 'SubGrid' ribbon and drag a new button into the 'Process' Group (Or in CRM2013 or later anywhere on the sub grid command bar).

    Subgrid

    Give your buttons an image, and select the Command 'new.contact.RunDialogGrid.Command'

  18. Select the 'Form' ribbon and drag a new button into the 'Process' Group (Or in CRM2013 or later anywhere on the form command bar).

    FormButton

    Give your buttons an image, and select the Command 'new.contact.RunDialogForm.Command'
  19. Click 'Publish'.
  20. You will now be able to run your chosen dialog from the homepage grid, form and sub-grids.

Feedback and Knowledge Base