DEV Community

Cover image for Model Driven App: disable "Open in Excel Online" button
Riccardo Gregori
Riccardo Gregori

Posted on

Model Driven App: disable "Open in Excel Online" button

πŸ€” The issue

The out-of-the-box Excel integration in Model Driven apps is a powerful, but very dangerous, feature.

Import from Excel, especially, can be very harmful because an user could leverage it to bypass any client side logic put on forms, generating inconsistent data within the system. Moreover, it cannot be customized: you cannot say "you can import opportunities records, but you cannot import accounts", neither you could say "import works only on columns A, B, C".

That's the reason why, in most of the CRM (and MDAs) implementations I've seen so far, the Import from Excel button is disabled for standard users, and replaced with custom data-import capabilities.

Disabling the Import from Excel button is quite easy, It just requires from you to remove the Data Import and Data Map privileges from the role assigned to users.

Data Import and Data Map privileges removed from roles

It works... more or less...

...even if the Data Import privilege is removed, you can have the possibility to open the "Export In Excel" > "Open in Excel Online" capability

Open in Excel Online

With the edit capability enabled, and the Save button visible and clickable:

Save button

If you change something and click on Save, the following (ugly) error message appears (depending on the UI language of the user, mine is Italian)

Error message

That's a behavior that looks more like a bug than a feature, IMHO.

The best would be to disable that "Open in Excel Online" button too... tipically for specific users/roles. But there is no such option in the role privileges...

πŸ˜– Failed fix tentatives

If you try to hide the button using Ribbon Workbench or the Modern Ribbon editor, you will quickly see that's any customization on that button is disabled.

Disabled

πŸ‘πŸ» How to do it

For people like me, mold by CRM implementations before Ribbon Workbench, the fix is quite straightforward: let's tweak the RibbonDiffXml "the old way".

First of all, let's create a solution containing only the definition of the entity we want to disable that button for.

Temp solution

Temp solution

Then let's use Power Platform CLI commands to export and unpack the solution in a local folder

pac solution clone --name temp
Enter fullscreen mode Exit fullscreen mode

It generates locally a structure similar to the following:

Exported solution

Let's open the RibbonDiff.xml file, it will be like the following one:

<?xml version="1.0" encoding="utf-8"?>
<RibbonDiffXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <CustomActions />
  <Templates>
    <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
  </Templates>
  <CommandDefinitions />
  <RuleDefinitions>
    <TabDisplayRules />
    <DisplayRules />
    <EnableRules />
  </RuleDefinitions>
  <LocLabels />
</RibbonDiffXml>
Enter fullscreen mode Exit fullscreen mode

In the <DisplayRules> node, let's add a rule that is evaluated to "true" only in the case an user is system administrator (here you can use your fantasy)...

<DisplayRules>
  <DisplayRule Id="greg.tablename.IsSystemAdmin.DisplayRule">
    <EntityPrivilegeRule PrivilegeType="Delete" PrivilegeDepth="Basic" EntityName="solution" Default="false" InvertResult="false" />
  </DisplayRule>
</DisplayRules>
Enter fullscreen mode Exit fullscreen mode

Then, in the <CommandDefinitions> node, let's add the following

<CommandDefinitions>
  <CommandDefinition Id="Mscrm.ExportToExcel.Online">
    <EnableRules>
      <EnableRule Id="Mscrm.ExportToExcel.ValidForXlsxExport" />
      <EnableRule Id="Mscrm.EnableExportToExcelOnlineForModern" />
      <EnableRule Id="Mscrm.EnableOnlyInBrowsersForModern" />
    </EnableRules>
    <DisplayRules>
      <DisplayRule Id="Mscrm.Live" />
      <DisplayRule Id="Mscrm.IsExportToExcelFCBEnabled" />
      <DisplayRule Id="Mscrm.HideOnOutlookClient" />
      <DisplayRule Id="Mscrm.NotAdvancedFind" />
      <DisplayRule Id="Mscrm.HideOnPhoneForNonModern" />
      <DisplayRule Id="Mscrm.HideForTabletExperience" />
      <DisplayRule Id="greg.tablename.IsSystemAdmin.DisplayRule" />
    </DisplayRules>
    <Actions>
      <JavaScriptFunction FunctionName="XrmCore.Commands.Export.exportToExcel" Library="$webresource:Main_system_library.js">
        <CrmParameter Value="SelectedControl" />
        <CrmParameter Value="SelectedEntityTypeName" />
        <IntParameter Value="6" />
      </JavaScriptFunction>
    </Actions>
  </CommandDefinition>
</CommandDefinitions>
Enter fullscreen mode Exit fullscreen mode

The final RibbonDiffXML should look something similar to the following:

<?xml version="1.0" encoding="utf-8"?>
<RibbonDiffXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <CustomActions />
  <Templates>
    <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
  </Templates>
  <CommandDefinitions>
    <CommandDefinition Id="Mscrm.ExportToExcel.Online">
      <EnableRules>
        <EnableRule Id="Mscrm.ExportToExcel.ValidForXlsxExport" />
        <EnableRule Id="Mscrm.EnableExportToExcelOnlineForModern" />
        <EnableRule Id="Mscrm.EnableOnlyInBrowsersForModern" />
      </EnableRules>
      <DisplayRules>
        <DisplayRule Id="Mscrm.Live" />
        <DisplayRule Id="Mscrm.IsExportToExcelFCBEnabled" />
        <DisplayRule Id="Mscrm.HideOnOutlookClient" />
        <DisplayRule Id="Mscrm.NotAdvancedFind" />
        <DisplayRule Id="Mscrm.HideOnPhoneForNonModern" />
        <DisplayRule Id="Mscrm.HideForTabletExperience" />
        <DisplayRule Id="greg.tablename.IsSystemAdmin.DisplayRule" />
      </DisplayRules>
      <Actions>
        <JavaScriptFunction FunctionName="XrmCore.Commands.Export.exportToExcel" Library="$webresource:Main_system_library.js">
          <CrmParameter Value="SelectedControl" />
          <CrmParameter Value="SelectedEntityTypeName" />
          <IntParameter Value="6" />
        </JavaScriptFunction>
      </Actions>
    </CommandDefinition>
  </CommandDefinitions>
  <RuleDefinitions>
    <TabDisplayRules />
    <DisplayRules>
      <DisplayRule Id="greg.tablename.IsSystemAdmin.DisplayRule">
        <EntityPrivilegeRule PrivilegeType="Delete" PrivilegeDepth="Basic" EntityName="solution" Default="false" InvertResult="false" />
      </DisplayRule>
    </DisplayRules>
    <EnableRules />
  </RuleDefinitions>
  <LocLabels />
</RibbonDiffXml>
Enter fullscreen mode Exit fullscreen mode

Now build the solution ad upload the generated solution zip file:

dotnet build
pac solution import --path .\bin\debug\temp.zip
Enter fullscreen mode Exit fullscreen mode

Wait for the import to complete, then go to your environment, select the solution you just uploaded, and click Publish All Customizations.

Publish all

Or, if you're using PACX in your project, simply type:

pacx publish all
Enter fullscreen mode Exit fullscreen mode

That's it... Now that button will be visible only by System Administrator.

Hope this helps you too!

Top comments (0)