Dynamics 365 Business Central: How to prevent uninstallation of extensions (Two ways)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to prevent uninstallation of extensions in Business Central.
Why is this topic mentioned? Because I recently heard a complaint from a customer because an end user accidentally uninstalled the AppSource extension, and then that extension disappeared from the Extension Management page, when installing it back again they needed to contact Partner (Not a free extension). Of course the initial problem point was that they didn’t manage the permissions well enough, why would they let the end user have the permission to uninstall the extension… Well, let’s not mention that for now.

This time I’ll start with a brief overview of the situation they encountered. For the PTE (Per-Tenant-Extension), if we uninstall it, it will still show up on the Extension Management page as Uninstall, and we can install it again. But for the AppSource extension, once you uninstall it, it disappears from the page and we can’t install it again.

Test Video: PTE -> AppSource Extension

PS: Same PTE may exist in multiple versions in the system. But only the latest version can be installed.

Cannot install the extension Designer PTE by YUN ZHU 1.0.0.1 because the tenant msasia4477t55724972 already uses a different version of it with the same app ID (3c8feeb6-213d-42de-a504-0fca67d989b6). Existing extension: Designer PTE by YUN ZHU.

You can unpublish these old PTEs that are not installed.

Let’s get back to the topic.

1. Using Permission Set

To install or uninstall extensions from AppSource or add per-tenant extensions, you must have the right permissions. You must either be a member of the D365 Extension Mgt. user group, or you must have the EXTEN. MGT. – ADMIN permission set explicitly.

So, to prohibit a user from uninstalling extensions, just remove the user from the EXTEN. MGT. – ADMIN permission set.

When a user without EXTEN. MGT. – ADMIN permission set tries to install or uninstall a extension, the following message will appear.

You do not have sufficient permissions to manage extensions. Please contact your administrator.

When installing:

When uninstalling:

Note: D365 EXTENSION MGT permission set is obsoleted.

2. Customization

As you know, we need to use standard features whenever possible, but using standard permission management, it is not possible to restrict only the uninstall permission. Is there a way to customize it?

First of all, we do not have the Codeunit subtype for Uninstall in the current version.

ValueDescription
NormalA normal codeunit. This is the default setting.
TestA test codeunit includes AL methods that test the application.
TestRunnerA test runner codeunit manages the execution of one or more test codeunits.
UpgradeAn upgrade codeunit includes AL methods for synchronizing changes to a table definition in an application with the business data table in SQL Server and migrating existing data.
InstallAn install codeunit includes AL methods for performing operations unconcerned with the extension code itself during the initial installation and the reinstallation of an extension.

And Table 2000000206 “Published Application” has scope ‘OnPrem’.

Neither Extension Management page nor Extension Details page can be extended.

The application object or method ‘Extension Management’ has scope ‘OnPrem’ and cannot be used for ‘Extension’ development.

Extension Management (2500, List)

Extension Details (2501, NavigatePage)

So how to do?
Here’s a little trick. Although it is set Extensible to be false, we can still extend it using Page trigger events.

codeunit 50102 PreventUninstallation
{
    [EventSubscriber(ObjectType::Page, Page::"Extension Management", 'OnBeforeActionEvent', 'Uninstall', false, false)]
    local procedure MyProcedure()
    begin
    end;

    [EventSubscriber(ObjectType::Page, Page::"Extension Details", 'OnBeforeActionEvent', 'Uninstall', false, false)]
    local procedure MyProcedure2()
    begin
    end;
}

Let’s look at a detailed example.

Create a new setting field (“Allow Uninstallation”; Boolean) in User Setup.

Add control code in ‘OnBeforeActionEvent’ of ‘Uninstall’ action for “Extension Details” page.

Test Video:

Worked well.😁 Give it a try!!!

Source Code:

codeunit 50102 PreventUninstallation
{
    [EventSubscriber(ObjectType::Page, Page::"Extension Details", 'OnBeforeActionEvent', 'Uninstall', false, false)]
    local procedure PreventUninstallation()
    var
        UserSetup: Record "User Setup";
    begin
        if UserSetup.Get(UserId) then
            if UserSetup."Allow Uninstallation" then
                exit;
        Error('You do not have permission to unintall the extension!!!');
    end;
}


tableextension 50114 UserSetupExt extends "User Setup"
{
    fields
    {
        field(50100; "Allow Uninstallation"; Boolean)
        {
            Caption = 'Allow Uninstallation';
            DataClassification = CustomerContent;
        }
    }
}
pageextension 50114 UserSetupPageExt extends "User Setup"
{
    layout
    {
        addafter("Allow Posting To")
        {
            field("Allow Uninstallation"; Rec."Allow Uninstallation")
            {
                ApplicationArea = All;
            }
        }
    }
}

PS:
1. Although the page can be extended using this way, the backend table (Scope = OnPrem) cannot be read. So we can’t run the code for a particular extension when it is uninstalled because we can’t get the extension ID of the currently uninstalled extension.

2. In addition to the Action event, other Page events can also be used.

For example,

END

Hope this will help.

Thanks for reading.

ZHU

コメント

タイトルとURLをコピーしました