Registering command on-the-fly

Commands can be registered via CommandContribution, similiar menu’s are registered via MenuContribution. Is it also possible to register Commands or Menus at a later moment, for example based on something a user did? And if so, how should that code look like? I would expect something like this:

...
{
    constructor(
        @inject(MenuModelRegistry) private readonly menuModelRegistry: MenuModelRegistry
    ) { }

    registerMenu(): void {
                this.menuModelRegistry.registerMenuAction(CommonMenus.EDIT_FIND, {
                    commandId: CommandIdd,
                    label: 'Custom menu entry'
                });
  }

Should this work? I tried it but did not see the menu item coming.
Thank you for your feedback.

@hbank I believe both commands and menus must be registered beforehand, as the registry must load and know about all available commands. Instead of registering commands at a later time, you should instead register them like other commands, and control their isVisible and/or isEnabled properties based on a user’s action.

@vince-fugnitto Thank you, This helps to understand it.

This is not true. Commands and actions can be registered at runtime as well.

1 Like

The same was asked a few days ago: MenuActions based on data recieved from backend

1 Like