Consume a web service in D365FO

How I started

The cornerstone of my solution was this Microsoft document.
https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/data-entities/consume-external-web-service
It is a step-by-step guide to help you understand the basics of web service consumption.
A good starting-point, but the code should be modified to your specific service as stated in the document.

How I implemented

In my case, I used the Hungarian National Bank’s web service to get the current exchange rates of HUF and store it in a table.
Following the steps below you can achieve the same:

1. Create a new Class Library project in Visual Studio.
• Visual Studio should open a new class by default, but do not type anything there just yet!

Create new Class Library

2. Add a new Service Reference to the References node of your project and insert the URL of the web service.

Add service reference

• As you can see, Visual Studio already shows all the usable methods of this service.

3. Create the following static method in the class that Visual Studio created.

• If EndpointAddress returns the error “The type or namespace name ‘EndpointAddress’ could not be found (are you missing a using directive or an assembly reference“, you can type “using System.ServiceModel;” under the other using references (at the top of your class), or you can click on EndpointAddress and press ‘Ctrl + .’, then add the reference by pressing ‘Enter’.

4. Build the solution. If the build succeeded, the .dll file should be in your project’s bin\Debug folder.

5. Once you have your .dll file, you can create a new Dynamics 365 Unified Operations project.

6. Add a new Reference to your solution and find the previously created file in the Browse section.

7. Create a new class and insert the following code:

This code loads the whole XML file returned by the .dll. The XML looks like this, and all the data we need is within the Rate’s attributes.

The while loop continues until there are no more “Rate” sections.
It stores the ‘unit’, ‘rate’ and ‘value’ in variables. These variables are then inserted into the MNBExchangeRates_DAX table I created.

Possible improvement of code functionality

• Modify the class to be a batch job and schedule it to call the service for example every morning. The exchange rates then would be stored in the table and it would be easy and fast to search certain dates.

Leave a Reply

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