Dynamics 365 Business Central APIs, Powershell, CSV and a bit of fancy notifications

In one of my last development workshops for Microsoft Italy, when talking about Dynamics 365 Business Central APIs, I’ve done a small demo on how to retrieve data from APIs directly from a Powershell script and save those data in a CSV file. Due to a lack of time, I’ve not talked too much about that script but (attendees will remember that) it ended in the following way:


This is a classical Windows 10 Toast notification, that automatically appears when something occours and it remains active in your notification’s area. Toast notifications are a part of the new modern UI of Windows 10 which are shown in rectangular shape using slide-in and slide-out animations. When I have some long running scripts I often use notifications like this.

To create such notifications with Powershell, I’ve used a Powershell module called BurnToast. This module permits you to create different types of toast notifications directly from Powershell (like simple toast, toast with logos and sounds, toast with reminders, toast with progress bar etc.).

To create a toast notification like the above image, is extremely simple. Just declare the text, header and logo of your toast and execute the New-BurntToastNotification cmdlet:

Install-Module BurntToast

$toastParams = @{
Text = "My notification message"
Header = (New-BTHeader -Id 1 -Title "My toast title")
AppLogo = "C:\YourLogo.png"

New-BurntToastNotification @toastParams

Extremely easy…

What about the Powershell script executed during my demo? It did the following:

  • Connects to the Dynamics 365 Business Central Customer API
  • Retrieves the data as JSON
  • Prints the retrieved data on screen
  • Saves the retrieved data on a CSV file
  • Shows a toast notification to notify that the job is finished and the CSV data export is ready to be used

If you know a bit of Powershell, I think that the script is quite self explanatory (and it could be write better for sure):

$user = 'YourD365BCUser';
$wsKey = 'YourD365BCWebServiceAccessKey';
$tenantID= 'YourTenantID';
$companyId = 'YourCompanyID';

$credentials = "$($user):$($wsKey)"
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credentials))
$headers = @{ Authorization = "Basic $encodedCredentials" }

$url = "https://api.businesscentral.dynamics.com/v2.0/" + $tenantID + "/production/api/v1.0/companies(" + $companyId + ")/customers";

$jsonResponse = Invoke-RestMethod -Uri $url -Method GET -Headers $headers | ConvertTo-Json | ConvertFrom-Json

$toastParams = @{
Text = "Customers retrieved from Dynamics 365 Business Central and data saved on CSV file"
Header = (New-BTHeader -Id 1 -Title "D365BC API Call from Powershell")
AppLogo = "C:\YourLogo.PNG"

Write-Host "Number of Customers retrieved: " $jsonResponse.value.Count

foreach ($cust in $jsonResponse.value)
Write-Host "$($cust.number) - $($cust.displayName)" 

#Save CSV file
$file = "C:\TEMP\data.csv"
$jsonResponse.value | Select number,displayName | ConvertTo-CSV -NoTypeInformation | Out-File $file

#Display a toast notification to the user
New-BurntToastNotification @toastParams

Complete script code is also available here.

Why I’ve presented this script? Not mainly for the fancy notification (obviously) but:

  • Because it shows how you can use Dynamics 365 Business Central APIs from Powershell
  • Because it shows a performant way to avoid using XMLport objects for exporting data from D365BC.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.