Let’s pass MB-820 – Episode 2: Use APIs for automation and administration

Share This Post

Hello everyone, welcome back!

This episode is part of the “Administer Dynamics 365 Business Central online – Training | Microsoft Learn” lesson and I will spend a few episodes over this lesson.

The lesson starts with an intro to Cloud Solution Provider, the place for partners to manage their BC customers and everything Microsoft. Once you have been setup as partner you can create customers, allow leads to login in trial environments and extend their trial if needed.

Next, the technical consultants and BC administrators can login in the BC admin center to administer sandboxes and Production environments.

When creating a new sandbox environment we have the options to choose the country and the platform version, and what we get is a dry database, meaning no companies no setup data, no master data.

We can create companies manually and setup the company with rapid start packages or manually.

However, one could use automatic APIs to:

  • create companies
  • install and un-install extensions
  • import and apply configuration packages
  • get permission sets
  • get profiles
  • get scheduled jobs
  • users: get users, get new users from AD and sync users with AD bound actions
  • get security group members

Automation API

This is the Microsoft Learn page related to automation API.

Use APIs for automation and administration – Training | Microsoft Learn

All these APIs below are obviously hosted on the Business Central cloud.

For example, Automation company has the following API endpoint:

GET /microsoft/automation/v2.0/companies({companyId})/automationCompanies({automationCompanyId})

But where is the API page? By inspecting the endpoint, we see the published is Microsoft and the APIGroup is ‘automation’.

If you didn’t have a chance to see my blog on “hidden extensions” it’s not late, check it out here:

Business Central: Discover hidden extensions and their content – Business Central Musings (svirlan.com)

Anyhow, the automation API is located (mostly) in _Exclude_APIV2_ extension.

To get access to the source code in _EXclude_APIV2_ extension add the extension in any project in app.json under the dependencies like below:

  "dependencies": [
    {
      "id": "10cb69d9-bc8a-4d27-970a-9e110e9db2a5",
      "name":  "_Exclude_APIV2_",
      "publisher":  "Microsoft",
      "version": "1.0.0.0"
    }
  ],

I said mostly because the Base App has one API as well. Searching for APIGroup = ‘automation’; yields the following results:

I used Postman to send the API requests to these automation APIs.

Before you start working with BC API in Postman you need to:

  • create an app registration in Azure/Entra portal
  • Give BC permissions to these new app registration
  • Create a secret
  • Add this app registration in BC, assigning BC permissions and granting consent.

If you need a refresher on how to execute above steps, check my previous blog out:

Using Postman to test OAuth 2.0 authorization to Business Central restful API – Business Central Musings (svirlan.com)

My plan for this blog is to write automation API requests using Postman:

  • create a new company
  • create a new package configuration
  • import it from binary file
  • Apply it

There are a few automation APIs to install/uninstall extensions which I will mention towards the end of this blog post.

Let’s get started:

  1. Create company:

POST

https://api.businesscentral.dynamics.com/v2.0/[TenantId]/[Environment]/api/microsoft/automation/v2.0/companies([ComanyId])/automationCompanies

Body:

{
    "name": "...",
    "displayName": "...",
    "businessProfileId": ""
}

2. Get last company created to Postman environment variable
GET https://api.businesscentral.dynamics.com/v2.0/11bf49b5-0d19-4952-94fd-3f41ad20af92/DEV/api/microsoft/automation/v2.0/companies?$filter=Name eq '...'

Post response script:

const jsonResponse = pm.response.json();
pm.environment.set("company_id", jsonResponse.value[0].id)

3. Create Package Configuration:

POST https://api.businesscentral.dynamics.com/v2.0/11bf49b5-0d19-4952-94fd-3f41ad20af92/DEV/api/microsoft/automation/v2.0/companies({{company_id}})/configurationPackages
Body:
{
    "code":"TESTAUTOMATION",
    "packageName": "Test Automation",
    "excludeConfigurationTables": true,
     "languageId": 1033
}

Post response scripts:
pm.environment.set("pack_id","");
pm.environment.set("pack_code", "");
const jsonResponse = pm.response.json();
pm.environment.set("pack_id",jsonResponse.id)
pm.environment.set("pack_code", "'"+jsonResponse.code+"'")

At the end of this request, your environment should have the following variables recorded:

4. Load package:

PATCH https://api.businesscentral.dynamics.com/v2.0/11bf49b5-0d19-4952-94fd-3f41ad20af92/DEV/api/microsoft/automation/v2.0/companies({{company_id}})/configurationPackages({{pack_id}})/file({{pack_code}})/content

5. Package import:

POST https://api.businesscentral.dynamics.com/v2.0/11bf49b5-0d19-4952-94fd-3f41ad20af92/DEV/api/microsoft/automation/v2.0/companies({{company_id}})/configurationPackages({{pack_id}})/Microsoft.NAV.import

6. Apply package:

POST https://api.businesscentral.dynamics.com/v2.0/11bf49b5-0d19-4952-94fd-3f41ad20af92/DEV/api/microsoft/automation/v2.0/companies({{company_id}})/configurationPackages({{pack_id}})/Microsoft.NAV.apply

At the end of this step we can verify the result in our sandbox:

A few more API requests re: extensions:

  1. Get all extensions:

GET https://api.businesscentral.dynamics.com/v2.0/%5BTenantId%5D/DEV/api/microsoft/automation/v2.0/companies(%5BCompanyId%5D)/extensions

2. Uninstall an extension:

POST https://api.businesscentral.dynamics.com/v2.0/%5BTenantId%5D/DEV/api/microsoft/automation/v2.0/companies(%5Bcompnayid%5D)/extensions(%5BextensionId%5D)/Microsoft.NAV.uninstall

3. Install extension:

POST https://api.businesscentral.dynamics.com/v2.0/%5BTenantId%5D/DEV/api/microsoft/automation/v2.0/companies(%5BCompanyId%5D)/extensions(%5BExtensionId%5D)/Microsoft.NAV.install

There are more automation extension and they will be a good exercise to practice and understand how automation API works in BC.

Watch my demo on automation API and a quick tour of BC Admin Center. Subscribe and stay in the loop for the next episodes:

Share This Post

Related Articles

Leave a Reply

Recent Posts

Get Notified About New Posts

Categories

Discover more from Business Central Musings

Subscribe now to keep reading and get access to the full archive.

Continue reading