Tuesday, May 19, 2015

Hide / Show Export to Excel button for specific Entity / Specific View

In MS CRM, Export to excel feature is enabled or disabled from security role.
You can have this feature for all applicable entities or none of the entity.
But sometime we have requirement to hide export to excel from particular entity, or show only for particular entity.
With Security role this requirement is not possible, but will little JavaScript code and ribbon customization we can achieve this.

Case 1: Hide Export to Excel button from contact Home grid.

1.      Create one custom solution
2.      Add Application Ribbons – from client Extensions

3.      Create one JavaScript web resource in this solution and add following code

function ShowHideExport2Excel(SelectedEntityTypeName) {
    if (SelectedEntityTypeName == 'contact')
        return false// hide button
        return true//show button

4.       Now Open this custom solution into Ribbon workbench
5.      In the Entities list you will see ApplicationRibbon
6.      Select Export to Excel button from Home, right click and Customize command

7.      In Commands, you will see Mscrm.ExportToExcel
8.      Now add custom Enable Rule to this command. Keep existing enable rules.
9.      When adding enable rule, add Custom JavaScript rule.
Default: True
FunctionName: ShowHideExport2Excel
invertResult: False
Library: your JavaScript library
Parameters :  [Crm Parameter] = SelectedEntityTypeName

10.  Add Crm Parameter to Custom JavaScript rule, value of this parameter must be SelectedEntityTypeName

11.   Save and publish your customization.
12.  Now Export to Excel button is hidden for Contact home grid.

Export To Excel button is not showing now.

 Export To Excel button is showing now.

Case 2: Show Export to Excel button only for contact Home grid.

In this case you just need to change JavaScript code little bit as

function ShowHideExport2Excel(SelectedEntityTypeName) {
    if (SelectedEntityTypeName == 'contact')
        return true// show button
        return false//hide button

Case 3: Show Export to excel button for particular entity and particular view only

[To do this need to use Unsupported JavaScript code]
To do this, we need to add one more Crm parameter to enable rule, and select value to this parameter is SelectedControl

And modify JavaScript function as

For MS CRM 2013 :

function ShowHideExport2Excel(SelectedEntityTypeName, selectedCtrl) {
    if (SelectedEntityTypeName == 'contact') {
        var view = selectedCtrl.get_$1X_3();
        var viewName = view.selectedViewName;
        if (viewName == 'My Active Contacts') //Change "My Active Contacts" with Your view name to show Export To Excel button
            return true; // show button
            return false; // hide button
        return true;

For MS CRM 2015:

function ShowHideExport2Excel(SelectedEntityTypeName, selectedCtrl) {
    if (SelectedEntityTypeName == 'contact') {        
        var viewName = selectedCtrl.get_viewTitle();
        if (viewName == 'My Active Contacts'//Change "My Active Contacts" with Your view name to show Export To Excel button
            return true// show button
            return false// hide button
        return true;

Wednesday, May 6, 2015

Running CRM Dialog from Custom Ribbon button

 1.      Add custom button on form
Say Run Dialog

     2. Now create command for custom button

     3. Add  Action to command

For the action add “Open Url Action”

3. For Url Command set your dialog URL
         To get dialog URL run dialog using “Start Dialog” button

Then select dialog you want to run on button click, once dialog started copy URL of dialog from address bar

 5. Your dialog url will be look like 

https://{CRM Server}:444/cs/dialog/rundialog.aspx?DialogId=%7b83D90936-EBEC-4CE5-8A58-A1530DE30680%7d&EntityName=new_test&ObjectId=%7b0FCB7F66-5FA6-E411-B1DF-0050568C6D7D%7d

It has Dialogid, Entityname and ObjectId
ObjectId is the record id on which this Dialog is running.

 6. If you use this URL as it is on button command then dialog will be run on same record again and again. 

      7.  To pass ObjectId / record id dynamically, need to pass one parameter to Url command, defined for custom button. This parameter should be like

    8. Now update your URL command address to

https://{CRM Server}:444/cs/dialog/rundialog.aspx?DialogId=%7b83D90936-EBEC-4CE5-8A58-A1530DE30680%7d&EntityName=new_test

Noticed that I removed &ObjectId=%7b0FCB7F66-5FA6-E411-B1DF-0050568C6D7D%7d, this objectId is passed by parameter, that’s why Parameter Name is important. 

    9.  Save and publish your customization.

   10.   Now dialog will be run for record you are in from button.

Tuesday, May 5, 2015

Convert FetchXML to SQL query

Some time we need to get SQL query for fetchxml. For MS CRM application you can get FetchXML‘s SQL query by running profiler on SQL server and find the query from profiler.
But if there are more users accessing CRM, then there are lot of queries are executing and we need to go through each query and need to relate to our fetchXML.
This tool converts existing entities system view FetchXML queries to SQL queries. Also you can enter directly fetch XML and it will convert to SQL query.

1.      Convert System View FetchXML to SQL query
a.      First select Entity from drop down
b.      Select system view, FetchXML of selected view will be converted to SQL query.
c.       Click on Get FetchXML, FetchXML for selected view will be retrieved from CRM and displayed in Textbox
d.     If required you can modify fetch xml as per requirement

e.      Click on Get SQL query, your FetchXML will be converted to SQL query and shown in textbox.

2.       Manual FetchXML to SQL
a.      Enter FetchXML in first text box, you want to convert into SQL query
b.      Click on Get SQL query, FetchXML converted and shown in textbox. 

User or Team filters
-          Based on current user all conditions are shown.

-          If any filter condition for user and/or userteam, then current user id and user teams will be retrieved and Id’s will be shown in SQL query.

Date time operator
-          Most of the time showing CRM database functions in SQL query. Because date time is varying based on user settings.

-          For some of the date time operators it is very hard to find exact value. So just showing what will be the possible values.

e.g. Operator  = last-x-fiscal-periods, every CRM might have different fiscal period setting, so user need to put those dates.

For Managed solution : https://fetchxml2sql.codeplex.com/