How to retrieve data from Dynamics 365 Business Central via Azure Function and Azure AD app and use it in public web form
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.
This is my form utilizing data from Dynamics 365 Business Central for populating the drop-down fields:
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 Office 365 tenant
- Expand the left side bar and navigate to Azure Active Directory:
- Go to Azure Active Directory → App registration → New registration:
- 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
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:
- Create a Function App
- Fork our repository to your GitHub account:
- Open the Function App and navigate to Deployment Center:
- In the Source step, select GitHub and sign into your account
- Select your fork
- Click Save
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
- Add the following properties to the Application settings list:
Dynamics365.BusinessCentral:AzureApp:ClientId
The Application (client) ID of the Azure AD appDynamics365.BusinessCentral:AzureApp:ClientSecret
The Client secret of the Azure AD appDynamics365.BusinessCentral:AzureApp:Tenant
Your Office 365 tenant, ex.: contoso.onmicrosoft.comDynamics365.BusinessCentral:Customers:Company
The name of the company in Dynamics 365 Business Central which Customers are requested in D365-BC-Customers functionDynamics365.BusinessCentral:Vendors:Company
The name of the company in Dynamics 365 Business Central which Vendors are requested in D365-BC-Vendors functionDynamics365.BusinessCentral:Items:Company
The name of the company in Dynamics 365 Business Central which Items are requested in D365-BC-Items function
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:
- 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
- Insert the function URL into your browser
- Sign in under a user with permissions to Dynamics 365 Business Central
- Accept the requested permissions:
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 CORS
- Add “*” to Allow Origins and save
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:
Next, I switch to JavaScript editor and specify data source for those fields:
And that is it:
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.