Consume API from Dynamics 365 Finance and Operations
Most companies are always ready to give you some integration work to do such as sending payment, moving file or sending file into D365FO.
In this demonstration, we will look at how we can consume a REST API from Dynamics 365FO using a C# project with X++ in Visual studio.
We can connect to APIs in D365FO and perform basic, GET, PUT, POST…. The basic thing is connect to the API and be able to talk with it. Web APIs are accessible through a url which we can refer to as $SiteName. Most of them require authentication which can be via a username and a password or via a key.
In this tutorial, we will connect to the boredapi service.
The API suggests random activities to do when bored.
The url to get activities is https://www.boredapi.com/api/activity/
TESTING WITH POSTMAN
Here, there is no authentication which means we can easily connect to the service and get random activities. I would first of all try to connect to this using postman.
I got a response.
CALL API FROM D365FO
Having tested with postman, now we can head to D365FO development environment to have some fun. To make a call from Visual studio, here are few steps:
-
Create a C# .NET Framework class library.
Here we will create a dll which will establish a connection with BoredAPI.
2. Create a method to get the activity
With the HttpClient we will connect to BroedAPI using the URL (siteName).
Here is the code sample to consume API in D365FO
using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace KWSBoredAPI { public class Helper { public static string getActivity(string request, string siteName = "https://www.boredapi.com/api/activity/", bool addHeader = false) { string retString = ""; HttpClientHandler clientHandler = new HttpClientHandler(); clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; }; // Pass the handler to httpclient(from you are calling api) HttpClient client = new HttpClient(clientHandler); client.BaseAddress = new Uri(siteName); client.DefaultRequestHeaders.Clear(); try { HttpResponseMessage response = client.GetAsync(request).Result; if (response.IsSuccessStatusCode) { retString = response.Content.ReadAsStringAsync().Result; } } catch (System.Exception ex) { retString = "issue with the call"; } return retString; } } }
As we can see, our dll is ready. You can check the result using a console app project.
3. Create an X++ project
Now, we will create a Runnable class to call the activity endpoint.
Let’s add the class library as a reference to our X++ project.
Right click project -> add -> reference
4. Create a runnable class
Next, we are running our class to see the outcome.
5. Run the class
Now we need to go visit some old teachers, lol.
I hope you enjoyed this.
Aurevoir!
Hi Komi,
Great article. I’m having trouble replicating this in my environment though. I have added a reference to the DLL in my D365 project but the compiler still doesn’t appear to recognize it. Is there a trick I’m missing? Any particular .Net framework I should be targeting?
I’m using VS2019 and targeting the default .NET 5 for my DLL.
Hi Stuart,
You should do this.
1. Clean & Build the DLL project with no error.
2. Remove reference from X++ Project
3. Comment where the reference had ben used.
4. REBUILD X++ Project
5. Readd reference and you should be fine.
Your comment is awaiting moderation.
Эта статья оказалась исключительно информативной и понятной. Автор представил сложные концепции и теории в простой и доступной форме. Я нашел ее очень полезной и вдохновляющей!