Custom Service Integration with D365 F&O

Custom Service Integration with D365 FO

Using customsServices is the most flexible and customizable way to integrate with D365FO. Custom services are created with standard D365FO X++ code and can be used for both data-based, and operation-based integrations.

In this blog, I have covered the steps to retrieve customers from D365 FO to the third-party applications. There are three parts specified below to execute the required:

  1. Create Custom Service on D365 FO
  2. Register the application on Azure Portal and add the same in D365 FO
  3. Access the service in third-party application

1. Create Custom Service on D365 FO

Below are the steps to create the custom service in D365 FO.

Create Dynamics Model and Project in visual studio

Create Service class

Add class “IntegrationServiceClass” with method below:

Method returning static Value:

Create Service

Right click on solution explorer and add a service named “IntegrationServices”. Set Properties as shown in the screen below:

Right click on Service Operations and add New Service Operations for the methods created in Service class.

New service operation

Set Name and Method for the created service operations.

created service operations.

Create Service Group

Add the Service Group named “IntegrationServicesGroup” and add a new service named “IntegrationServices”. Set Auto Deploy to “Yes.”

Service group

Build the project.

If you get an error such as the one specified in the screen below, update the model and add the “Directory” package. to solve the error of “Dynamics.AX.Directory”.

AXDirectory

Update your model and add “ContactPerson” Package to solve the error of “Dynamics.AX.ContactPerson”.

Dynamics.AX.ContactPerson

Build the project. On building the project, service will be automatically deployed.

2. Verify service on D365 FO URL

Service URL Format: https://<d365fourl>/api/services/<servicegroupname>/<servicename>/<operation_name>

In my case, below will be the service URL

https://usnconeboxax1aos.cloud.onebox.dynamics.com/api/services/IntegrationServicesGroup/IntegrationServices/getTest

https://usnconeboxax1aos.cloud.onebox.dynamics.com/api/services/IntegrationServicesGroup/IntegrationServices/getCustomer

3. Register the application on Azure Portal and add the same in D365 FO

Here are the steps to register the application on Azure Portal and add that application on D365 FO.

New Registration

Login on https://portal.azure.com/ using your credentials. Select Azure Active Directory >> App Registration.

New Registration

Click on “New Registration”. Enter details as shown in the screen below and register the application. Select Redirect URL to Web to authorize the application using client Id and secret. In case of selecting Native, the application will be authorized with the user credentials.

register the application

Generate Secret Key

To create a secret key, select certificates & secrets. Click on New client secret.

Enter details as shown in the screen below and Add the client secret.

Copy the client secret value

client secret value

Secret key value:

This value will not be visible again so do not miss to copy this.

API Permissions

To add permissions, click on API Permissions >> Add a permission

Add a permission >> Microsoft APIs >> Dynamics ERP.

Permission will be added as shown in the screen below.

Add Application in Dynamics AX FO

System Administration >> Azure Active Directory applications >> New

Enter Azure application client id in Application Id, application name and user id.

4. Access the service in third-party application

We will access the developed service in the .net application. Before developing the .net application, we will have to create the Authentication application for the security purpose.

AuthenticationUtility Application

Create a .net application for AuthenticationUtility. Add two classes “ClientConfiguration.cs” and “OAuthHelper.cs”.

You can copy the code in these two classes from Microsoft Dynamics AX Integration GitHub repository.

Microsoft Dynamics AX Integration GitHub repository

Below is the code of clientConfiguration, I have updated for my Integration Services.

Add “OAuthHelper.cs” class.

Before adding this class add directory for Microsoft.IdentityModel.Clients.ActiveDirectory from Tools >> NuGet Package Manager >> Package Manager Console with below command.

Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 5.2.8

Add OAuthHelper code as below and build the application.

Third Party Application

Create a new .net application “dev_ConsumeIntegrationServices”. Add reference to AuthenticationUtility.dll in this application.

Add Web Form with TextBox and Button to access service of AX as shown below:

Before applying the code, add references shown below:

Microsoft.VisualStudio.QualityTools.UnitTestFramework

To use Newtonsoft.Json library, install package Install-Package Newtonsoft.Json -Version 12.0.3 from Tools >> NuGet Package Manager >> Package Manager Console

Below is the code to access services of AX in the .net application:

Wrapping Up

What do you think? The d365 f&o integration was quite simple, right? With a bit of an understanding and a planned approach, we could ace any technical concept easily. Implement this and meanwhile, wait for our next resourceful tutorials on dynamics 365 finance and operations.

The following two tabs change content below.

Leave a Reply

Your email address will not be published. Required fields are marked *