logo
Forms
May 08, 2020

How to retrieve data from Dynamics 365 Business Central via Azure Function and Azure AD app and use it in public web form

Co-Founder at Plumsail

In the previous article, I’ve demonstrated how to provide public access to specific SharePoint data via Azure Function and use it in a public form. Here, I want to show how to do the same with Dynamics 365 Business Central. In this example, I will use data from Customers, Vendors, and Items lists in a public web form designed with Plumsail Forms. But with minor adjustments of the Azure Function, you can retrieve other lists available via Microsoft Graph API including Accounts, Employees, Journal and others. You can also configure cascading drop-downs or filter data in any other way.

1

This is my form utilizing data from Dynamics 365 Business Central for populating the drop-down fields:

2

Registration of Azure AD app

First, we need to register a new Azure AD app with access to financials data via Microsoft Graph API.

  • Open the Admin center of your Microsoft 365 tenant
  • Expand the left side bar and navigate to Azure Active Directory:

3

  • Go to Azure Active Directory App registration New registration:

4

  • Give the app an arbitrary name, ex.: FinancialData
  • Leave the Supported account types on the default setting of Accounts in this organizational directory only
  • Click Register
  • Copy the Application (client) ID of the app. We will use it in Azure Function

5

Now, we need to configure permissions for the app:

  • Navigate to API Permissions and click Add a permission
  • Under the Microsoft APIs tab, select Microsoft Graph
  • Choose Delegated permissions since Microsoft Graph API does not support Application permissions for Dynamics 365 Business Central at least at the time of writing this article
  • Select Financials Financials.ReadWrite.All
  • Click Add permissions

Finally, we need to create a client secret for our app to use it in Azure Function:

  • Navigate to Certificate & secrets and click New client secret
  • Enter any description, specify lifetime, click Add
  • Copy the created secret key

Deployment of Azure Function

You can deploy functions for retrieving data from Dynamics 365 Business Central directly from our GitHub repository. No need to modify source code to specify the app and data source properties, all of these can be configured via the Function App settings after the deployment:

6

  • Open the Function App and navigate to Platform features Container settings (Code Deployment):

7

  • In the Source control step, select GitHub and sign into your account, click Continue
  • In the Build provider step, select App Service build service. Click Continue
  • In the Configure step, select your copy of the data-source-functions repository
  • Click Finish

Once the project is built and deployed, you will see the functions in your Function App. Now, we need to configure it by specifying the Azure AD app properties and the company name from Dynamics 365 Business Central you want to retrieve data from.

  • Open the Function App and navigate to Configuration

8

  • Add the following properties to the Application settings list:
Dynamics365.BusinessCentral:AzureApp:ClientId
The Application (client) ID of the Azure AD app

Dynamics365.BusinessCentral:AzureApp:ClientSecret
The Client secret of the Azure AD app

Dynamics365.BusinessCentral:AzureApp:Tenant
Your Microsoft 365 tenant, ex.: contoso.onmicrosoft.com

Dynamics365.BusinessCentral:Customers:Company
The name of the company in Dynamics 365 Business Central which Customers are requested in D365-BC-Customers function

Dynamics365.BusinessCentral:Vendors:Company
The name of the company in Dynamics 365 Business Central which Vendors are requested in D365-BC-Vendors function

Dynamics365.BusinessCentral:Items:Company
The name of the company in Dynamics 365 Business Central which Items are requested in D365-BC-Items function

9

You might noticed the D365-BC-Authorize function among the others. Since Microsoft Graph API does not support app-only access to Dynamics 365, we had to create a separate function for providing permissions to Dynamics on behalf of a specific user. Before starting using other functions, you must call D365-BC-Authorize function and approve the requested permissions under a user with access to Dynamics 365 Business Central. You have to go through this step just once to allow end-users to call other functions anonymously without extra authentication. The Function App saves your credentials in cache for using in future requests:

  • Copy URL to the D365-BC-Authorize function:

10

  • Open the Azure AD app we created previously and navigate to Authentication section
  • Click Add platform, select Web, and insert the function URL into Redirect URIs text box
  • Click Configure

11

  • Insert the function URL into your browser
  • Sign in under a user with permissions to Dynamics 365 Business Central
  • Accept the requested permissions:

12

Finally, we need to enable Cross-Origin Resource Sharing (CORS) for our app to allow JavaScript requests from our web form:

  • Open the Function App and navigate to Platform features CORS
  • Add “*” to Allow Origins and remove all other values

13

Designing public web form

Finally, we can design a public web form and populate some fields from Azure Functions with JavaScript. In this example, I have Commercial proposal form with Customer, Vendors, and Items fields which I want to be populated from the corresponding functions.

First, I need to copy their names in the design mode:

14

Next, I switch to JavaScript editor and specify data source for those fields:

fd.rendered(function() {
    var customerWidget = fd.field('Customer').widget;
    customerWidget.setDataSource({
            transport: {
                read: '-- URL of D365-BC-Customers function --'
            }          
        });
    customerWidget.setOptions({dataTextField: 'displayName', dataValueField: 'displayName'});
    
    var vendorsWidget = fd.field('Vendors').widget;
    vendorsWidget.setDataSource({
            transport: {
                read: '-- URL of D365-BC-Vendors function --'
            }          
        });
    vendorsWidget.setOptions({dataTextField: 'displayName', dataValueField: 'displayName'});
    
    var itemsWidget = fd.field('Items').widget;
    itemsWidget.setDataSource({
            transport: {
                read: '-- URL of D365-BC-Items function --'
            }          
        });
    itemsWidget.setOptions({dataTextField: 'displayName', dataValueField: 'displayName'});
});

And that is it:

15

Summary

Previously, I’ve already demonstrated how to retrieve data from SharePoint Online. Now, you know how to do that with Dynamics 365 Business Central. If you have any specific requirements for your public web forms in part of working with external data or you want me to demonstrate integration with other services, just describe it in the comments. I’d be more than happy if my articles could help you making more functional and efficient forms. So do not hesitate to leave your questions.

Note: The post has been originally published at: