MSBA-271 · Lesson 3 of 12 · 19 min

Command bar customization

▶ Video coming soon A video version of this lesson is on the way.

Bronwyn's request from the last session was simple to say: a Mark In Progress button on the work order list, so that she could select several jobs and move them on without opening each one. Delphine wanted a Print Job Sheet button on the form, and Maribeth wanted the default Deactivate button hidden from transcript officers, who kept using it by mistake. All three were command bar changes, and the session was about where they live and how to build them without breaking the buttons Microsoft maintains.

What commands are

The command bar is the row of buttons at the top of a form, a list or a subgrid in a model-driven app. Each button is a command: a label and icon, an action to run when it is selected, and rules that decide whether it is visible and enabled. Microsoft ships many commands with every table, such as New, Save, Delete, Deactivate and Assign, and solutions such as Dynamics 365 Sales add their own. Developers and makers add custom commands and change or hide existing ones.

Commands appear in four places, called command bar locations, and the same logical action often needs a button in more than one of them.

Four panels, each with a colored bar at the top. Main grid command bar, over a list of rows in a view. Main form command bar, over one open row. Subgrid view command bar, over related rows on a form. Associated view command bar, over related rows on their own page.
The four command bar locations.
Command bar locations and what commands act on.
LocationWhere users see itWhat the command usually acts on
Main gridAbove a list of rows opened from the app's navigationThe selected rows, or the whole view
Main formAbove an open rowThe open row
Subgrid viewAbove a subgrid on a formSelected related rows, with the parent row as context
Associated viewAbove the list of related rows opened from the form's Related tabSelected related rows

Bronwyn's button belonged in the main grid, because she wanted to act on several selected rows. Delphine's belonged on the main form. Maribeth's change was to an existing command, Deactivate, on the transcript request form and grid.

Classic and modern commands

There are two ways commands are defined, and a developer meets both. Classic commands are defined in ribbon XML, stored in the RibbonDiffXml of a table or application ribbon and carried in solutions. Their actions are JavaScript functions, and their visibility is controlled by display rules and enable rules, some of which call JavaScript. For years they were edited by hand in solution XML or with the community tool Ribbon Workbench, because Microsoft provided no designer.

Modern commands are created in the command designer, opened from the modern app designer for a table and a location. A modern command has a label, an icon, a tooltip and an action, which can be a Power Fx formula or a JavaScript function in a web resource. Its visibility can be set to always show, to show on a condition from a Power Fx formula, or to use classic rules. Modern commands are stored as app elements and travel in solutions with the app.

Two columns. Classic commands: ribbon definitions in solution XML, JavaScript actions and rules, edited with community tools, still run in every app. Modern commands: command designer in the app designer, Power Fx or JavaScript actions, visibility by formula or classic rules, stored per app with a component library.
Classic and modern commands side by side.

Both kinds run side by side, and Microsoft has been migrating its own commands to the modern infrastructure incrementally. Some limits matter in practice. Pre-existing classic commands, including many of Microsoft's own, cannot be edited in the command designer until they have been migrated. The global application header and dashboard command bars are not supported in the command designer. Dynamically populated buttons, the kind classic ribbons built from code at run time, are not supported in modern commands; they must be declared. And modern commands created in one app cannot be added to a different app, though they can be copied and pasted.

Choosing between modern and classic customization.
SituationUse
A new button for one app, with simple logicModern command with a Power Fx action
A new button that must call a custom API or open a custom page with parametersModern command with a JavaScript action, or Power Fx where it is enough
Hiding or changing an existing Microsoft command that is still classicClassic ribbon customization, carefully and in a solution
A command on the global header or a dashboardClassic ribbon customization; the designer does not support these bars
A button whose items are built from data at run timeRedesign; modern commands do not support dynamic buttons

Building a modern command

Wiremu built Bronwyn's button in the command designer during the session. In the app designer for Facilities Hub he opened the Work Order table, chose Edit command bar, selected the Main grid location and added a new command. He gave it the label Mark In Progress, an icon from the built-in set, a tooltip and an accessibility label, then chose Run formula as the action. The first time a Power Fx command is added to an app, the designer creates a command component library for that app, which holds the formulas and the data sources they use. Lesson 4 covers the formulas themselves.

For the visibility rule he chose Show on condition from formula, so that the button appears only when at least one row is selected and every selected row is in a state where moving to In Progress makes sense. He then published the app. Commands are part of the app, so the change reached users only after publishing and, beyond Wiremu Dev, only after the solution moved through Lumenvale's pipeline.

Two details about where commands are edited caught Wiremu out. Editing commands from the Solutions or Tables areas of the maker portal does not offer Run formula as an action or Show on condition from formula as a visibility rule; those options are available only from the modern app designer. And only one command component library is supported per app. An app that somehow gains a second, duplicate library shows an error when the command designer opens, and the fix is to remove the extra app element.

JavaScript actions

Some actions need more than Power Fx in commands can do: calling a custom API, opening a custom page with several parameters, or reusing a function that the form already uses. A modern command can run a JavaScript function instead. The command names a web resource library and a function, and passes parameters such as PrimaryControl, which is the form context on a form or the grid control on a grid, and SelectedControlSelectedItemIds, the IDs of selected rows. The function receives those parameters in order.

A JavaScript command action on the main form. The web resource name is an example.
var Lumenvale = window.Lumenvale || {};
Lumenvale.WorkOrderCommands = {
    printJobSheet: async function (primaryControl) {
        var formContext = primaryControl;          // on a form, PrimaryControl is the form context
        if (formContext.data.entity.getIsDirty()) {
            await formContext.data.save();          // make sure the sheet shows saved values
        }
        var id = formContext.data.entity.getId().replace(/[{}]/g, "");
        return Xrm.Navigation.navigateTo(
            { pageType: "webresource", webresourceName: "lcc_/pages/jobsheet.html", data: "id=" + id },
            { target: 2, width: { value: 70, unit: "%" }, title: "Job sheet" });
    }
};

For Delphine's Print Job Sheet button, Wiremu reused an HTML web resource that formats one job for printing, opened as a dialog with navigateTo. The function saves first if the form has unsaved changes, so the printed sheet never disagrees with the screen. That kind of check is easier in JavaScript than in Power Fx, which is why he chose a JavaScript action here.

Hiding and changing existing commands

Maribeth's request, hiding Deactivate from transcript officers, is the most common command change and the one most likely to go wrong. There are three safe options and one unsafe one. The best is often not to hide the command at all but to remove the privilege behind it: if officers should not deactivate requests, their security role should not allow it, and Microsoft's commands already hide or disable themselves when the user lacks the privilege. That also stops deactivation through every other route.

If the privilege must stay, because officers need it in another app, a modern app can hide the command for that app. In the command designer, Microsoft's commands that have been migrated can be hidden in a location for one app. For commands that are still classic, hiding means a classic ribbon customization, which applies to the table in every app unless its rules check the app. The unsafe option is to delete or replace Microsoft's command definitions wholesale in ribbon XML, which breaks when Microsoft updates the command and can leave users with no way to do their job.

Maribeth checked with Rupert. Officers did not need to deactivate requests anywhere, so the change was made in the Transcript Officer role, not in the command bar. The button disappeared for officers and stayed for the registrar's supervisors, who held a different role.

Worked problem

Three requests, three designs

Kwabena asks for an Export to Research button on the Inspection list that calls a custom API for the selected rows; Octavia asks for a Request Quote button on the work order form for external jobs only; and Delphine asks for the standard Assign button to be hidden on the work order grid for technicians. How should each be built?

Show solution
  1. Export to Research acts on selected rows and calls a custom API. A modern command on the main grid with a JavaScript action receiving SelectedControlSelectedItemIds can call the API through Xrm.WebApi.execute. Visibility: only when rows are selected.
  2. Request Quote applies to one open row and only for external jobs. A modern command on the main form, with a visibility formula that checks the Category column, and an action that opens a custom page or creates a quote request row.
  3. Hiding Assign for technicians: first ask whether technicians should be able to assign at all. If not, remove the Assign privilege on work orders from the Facilities Technician role, and the command hides itself. Only if they need the privilege elsewhere should the command be hidden in this app.

Answer: A JavaScript grid command, a Power Fx form command with a visibility formula, and a security role change rather than a command change.

Commands and ALM

Commands are solution components, and they need the same discipline as code. Modern commands are stored with the app, so the app must be in the solution being moved; the command component library is also an app element carried in the solution. JavaScript actions depend on their web resources, which must be in the same solution or a solution it depends on. Classic ribbon changes are stored per table or for the application ribbon and are merged across solutions in layers, which makes them harder to reason about. Lumenvale kept all ribbon changes for a table in one solution, owned by one developer, to avoid surprises when layers combined.

Rupert added one governance point. A command is often the most visible thing a user notices after a release, so every new command needed a label, a tooltip and an accessibility label written for the people using it, reviewed by the business owner before production. A command called btnMIP_v2 had reached the registrar's test environment the previous spring and been reported as a bug by three users.

Organizing commands for users

A command bar with too many buttons is as unhelpful as one with too few. Modern commands can be arranged as single commands, dropdowns that hold related commands, groups that separate sets of commands inside a dropdown, and split buttons that run a default action with alternatives beside it. Kwabena's planned export commands, for example, fitted one dropdown named Export rather than three separate buttons. Order matters too: the commands people use most should come first, because on narrow screens the rest move into an overflow menu.

Labels and icons need the same care as any interface text. Each command needs a short, specific label, such as Print job sheet rather than Print, and a tooltip that explains anything the label cannot. Icons should support the label, not replace it. Commands that change data should say so plainly, and destructive ones should confirm, as lesson 4 shows. Screen reader users hear the label, so a command whose meaning depends on its icon is a barrier.

The facilities and registrar command requests and how each was met.
RequestLocationApproach
Print job sheet for one work orderMain formModern command with a JavaScript action
Assign selected work orders to a technicianMain gridModern command with Power Fx, visible when rows are selected
Hide Deactivate on transcript requestsMain form and gridHide the command, or remove the privilege from the role
Export to researchMain gridModern command in an Export dropdown

Common mistakes

  • MistakeHiding a command to stop people doing something they must never do.

    InsteadRemove the privilege in their security role; hiding a button does not stop other routes.

  • MistakeLooking for Run formula in the Solutions area.

    InsteadEdit commands in the modern app designer to use Power Fx actions and visibility formulas.

  • MistakeReplacing Microsoft's command definitions wholesale in ribbon XML.

    InsteadHide or override specific commands with rules, and keep changes small and in one solution.

  • MistakeForgetting the web resource for a JavaScript action.

    InsteadAdd the library to the same solution, or a solution the app depends on.

  • MistakeExpecting a modern command to appear in another app.

    InsteadModern commands belong to one app; copy and paste them into others.

Summary and key terms

  • Commands are buttons with an action and visibility rules, shown in four locations: main grid, main form, subgrid view and associated view.
  • Classic commands are ribbon XML with JavaScript actions and rules; modern commands are built in the command designer with Power Fx or JavaScript actions.
  • The command designer cannot edit unmigrated classic commands, the global header or dashboards, or dynamic buttons, and modern commands belong to one app.
  • Power Fx actions and visibility formulas are available only from the modern app designer, which creates one command component library per app.
  • JavaScript actions receive parameters such as PrimaryControl and SelectedControlSelectedItemIds.
  • To stop an action, change the security role; hide commands only for usability.
Command
A button on a command bar with an action and rules for visibility.
Command bar location
One of the places commands appear: main grid, main form, subgrid view or associated view.
Classic command
A command defined in ribbon XML with JavaScript actions and display or enable rules.
Modern command
A command built in the command designer with a Power Fx or JavaScript action.
Command component library
The app-level library created by the command designer to hold Power Fx command formulas and their data sources.
PrimaryControl
A command parameter that passes the form context on a form, or the grid control on a grid.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. Why did Maribeth change a security role instead of hiding Deactivate?

    Show answer

    Officers should never deactivate requests; removing the privilege stops every route and Microsoft's command hides itself without customization.

  2. Which parameter passes the selected rows' IDs to a JavaScript command action?

    Show answer

    SelectedControlSelectedItemIds.

  3. Name two command bars the command designer does not support.

    Show answer

    The global application header and dashboard command bars.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz