Dynamics 365 Business Central: Can we get all environments within BC via AL (Not in Admin Center)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to briefly discuss a question that has been asked recently, can we get all environments within BC via AL (Not in Admin Center)?

The Business Central administration center provides a portal for administrators to do administrative tasks for a Business Central tenant. Administrators can view and work with production and sandbox environments on the tenant.

And from 2022 release wave 2, we can switch company in the current browser tab or open the selected company in a new tab to multitask or compare data side by side.
PS: Business Central 2022 wave 2 (BC21): Switch companies across environments

For the current environment, we can use codeunit 457 “Environment Information” to get the environment name.

More details: Dynamics 365 Business Central: How to quickly get the current environment name, environment type, and deployment type via AL

But we cannot get information about other environments. So is there any way to do it? Yes there is, but there is no very simple way. So far, we can only use Admin Center API.
PS: Dynamics 365 Business Central: Manage environments via Admin Center API

The Business Central administration center API supports authentication using Microsoft Entra apps now. (This authentication type is supported from version 20.0 an onwards)

The method is similar to the OAuth we mentioned earlier, and only some key steps are pointed out here, more details: Using OAuth to connect Business Central APIs and Web Service in Postman

Grant the registered application AdminCenter.ReadWrite.All permission to the Dynamics 365 Business Central administration center API as follows:

  1. Select API permissions > Add a permission > Microsoft APIs.
  2. Select Dynamics 365 Business Central.
  3. Select Application permissions, select AdminCenter.ReadWrite.All, then select Add permissions.

Go to the Business Central administration center and navigate to the ‘Authorized Microsoft Entra apps’ page. Paste the Application (client) ID of your app in the form to authorize an app.

More details: The Business Central Admin Center API

Test endpoint:

https://api.businesscentral.dynamics.com/admin/v2.3/applications/businesscentral/environments

Great.

{
    "value": [
        {
            "friendlyName": "2qcj3x-SandboxUS",
            "type": "Sandbox",
            "name": "SandboxUS",
            "countryCode": "US",
            "applicationFamily": "BusinessCentral",
            "aadTenantId": "7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62",
            "applicationVersion": "25.3.28755.30175",
            "status": "Active",
            "webClientLoginUrl": "https://businesscentral.dynamics.com/7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62/SandboxUS",
            "webServiceUrl": "https://api.businesscentral.dynamics.com/v2.0/7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62/SandboxUS",
            "locationName": "East US",
            "platformVersion": "25.2",
            "databaseSize": {
                "value": 425984.0,
                "unit": "Byte"
            },
            "ringName": "Production",
            "appInsightsKey": ""
        },
        {
            "friendlyName": "2qcj3x",
            "type": "Production",
            "name": "Production",
            "countryCode": "US",
            "applicationFamily": "BusinessCentral",
            "aadTenantId": "7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62",
            "applicationVersion": "25.0.23364.30130",
            "status": "Active",
            "webClientLoginUrl": "https://businesscentral.dynamics.com/7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62/Production",
            "webServiceUrl": "https://api.businesscentral.dynamics.com/v2.0/7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62/Production",
            "locationName": "South Central US",
            "platformVersion": "25.0",
            "databaseSize": {
                "value": 425984.0,
                "unit": "Byte"
            },
            "ringName": "Production",
            "appInsightsKey": ""
        },
        {
            "friendlyName": "2qcj3x-Sandbox254",
            "type": "Sandbox",
            "name": "Sandbox254",
            "countryCode": "JP",
            "applicationFamily": "BusinessCentral",
            "aadTenantId": "7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62",
            "applicationVersion": "25.4.29661.30648",
            "status": "Active",
            "webClientLoginUrl": "https://businesscentral.dynamics.com/7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62/Sandbox254",
            "webServiceUrl": "https://api.businesscentral.dynamics.com/v2.0/7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62/Sandbox254",
            "locationName": "Japan East",
            "platformVersion": "25.2",
            "databaseSize": {
                "value": 753664.0,
                "unit": "Byte"
            },
            "ringName": "Production",
            "appInsightsKey": ""
        },
        {
            "friendlyName": "2qcj3x-Sandbox253",
            "type": "Sandbox",
            "name": "Sandbox253",
            "countryCode": "JP",
            "applicationFamily": "BusinessCentral",
            "aadTenantId": "7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62",
            "applicationVersion": "25.3.28755.30647",
            "status": "Active",
            "webClientLoginUrl": "https://businesscentral.dynamics.com/7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62/Sandbox253",
            "webServiceUrl": "https://api.businesscentral.dynamics.com/v2.0/7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62/Sandbox253",
            "locationName": "Japan East",
            "platformVersion": "25.2",
            "databaseSize": {
                "value": 770048.0,
                "unit": "Byte"
            },
            "ringName": "Production",
            "appInsightsKey": ""
        }
    ]
}

This is in JSON format and we can get them via AL in one of the BC environments.
PS: Dynamics 365 Business Central: How to use OAuth 2.0 in AL with SecretText (Using codeunit 501 OAuth2)

Let’s see more details.
First I create a new table and page to display the same content as in Admin Center.

As a test I set the page to allow deleting data, you can disable it if you want.

Then add a action on the new page to get the BC environment list. (You can also update this data every time you open the page)

Logic part
Authenticate:

HTTP Method GET:

JSON file processing and inserting data:

Done:

Great, give it a try!!!😁

Source Code: GitHub (Please note that the source code is for reference only, you can improve it according to your own needs)

In addition, if you don’t want to use the APIs, there is an Environments page in the Company Hub feature where you can manually add these environments.
More details: Company Hub in Dynamics 365 Business Central 2020 Release Wave 2

PS:
1. Dynamics 365 Business Central: How to get the Azure Active Directory tenant domain name and ID via AL (codeunit 433 “Azure AD Tenant”)

2. Dynamics 365 Business Central mini tips: How to get Tenant GUID in AL (Not Tenant ID)

3. How to access Dynamics 365 Business Central admin center from the URL without Tenant ID

4. Dynamics 365 Business Central: How to get Microsoft Entra tenant registered country letter code and preferred language in AL

5. Dynamics 365 Business Central: How to check BaseApp version via AL (Include First-party localization)

6. Dynamics 365 Business Central: How to get the full version text in Help & Support via AL (Record daily version information)

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL