{"id":660,"date":"2020-01-07T11:09:34","date_gmt":"2020-01-07T10:09:34","guid":{"rendered":"http:\/\/daxvisionerp.com\/?p=660"},"modified":"2025-10-27T08:14:14","modified_gmt":"2025-10-27T08:14:14","slug":"consume-a-web-service-in-d365fo","status":"publish","type":"post","link":"https:\/\/daxvisionerp.com\/home\/consume-a-web-service-in-d365fo\/","title":{"rendered":"Consume a web service in D365FO"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">How I started<\/h3>\n\n\n\n<p>The cornerstone of my solution was this Microsoft document.<br><a href=\"https:\/\/docs.microsoft.com\/en-us\/dynamics365\/fin-ops-core\/dev-itpro\/data-entities\/consume-external-web-service\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.microsoft.com\/en-us\/dynamics365\/fin-ops-core\/dev-itpro\/data-entities\/consume-external-web-service<\/a><br>It is a step-by-step guide to help you understand the basics of web service consumption. <br>A good starting-point, but the code should be modified to your specific service as stated in the document.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How I implemented<\/h3>\n\n\n\n<p>In my case, I used the Hungarian National Bank\u2019s web service to get the current exchange rates of HUF and store it in a table.<br>Following the steps below you can achieve the same:<\/p>\n\n\n\n<p>1. Create a new Class Library project in Visual Studio.<br> \u2022    Visual Studio should open a new class by default, but do not type anything there just yet!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"http:\/\/daxvisionerp.com\/wp-content\/uploads\/2020\/01\/image-18.png\" alt=\"\" class=\"wp-image-661\"\/><figcaption class=\"wp-element-caption\">Create new Class Library<\/figcaption><\/figure>\n\n\n\n<p>2. Add a new Service Reference to the References node of your project and insert the URL of the web service.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" src=\"http:\/\/daxvisionerp.com\/wp-content\/uploads\/2020\/01\/image-19.png\" alt=\"\" class=\"wp-image-662\"\/><figcaption class=\"wp-element-caption\">Add service reference<\/figcaption><\/figure>\n\n\n\n<p>\u2022    As you can see, Visual Studio already shows all the usable methods of this service.<\/p>\n\n\n\n<p>3. Create the following static method in the class that Visual Studio created.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>public static string GetExchRate()\n{\n   var binding = new System.ServiceModel.BasicHttpBinding();\n   var endpointAddress = new EndpointAddress(\"http:\/\/www.mnb.hu\/arfolyamok.asmx\");\n   MNBExchRateReference.MNBArfolyamServiceSoapClient client = new MNBExchRateReference.MNBArfolyamServiceSoapClient(binding, endpointAddress);\n   MNBExchRateReference.GetCurrentExchangeRatesRequestBody request = new MNBExchRateReference.GetCurrentExchangeRatesRequestBody();\n   client.Close();\n   return Convert.ToString(client.GetCurrentExchangeRates(request).GetCurrentExchangeRatesResult);\n} <\/code><\/pre>\n\n\n\n<p>\u2022 If EndpointAddress returns the error \u201cThe type or namespace name &#8216;EndpointAddress&#8217; could not be found (are you missing a using directive or an assembly reference\u201c, you can type &#8220;using System.ServiceModel;&#8221; under the other using references (at the top of your class), or you can click on EndpointAddress and press \u2018Ctrl + .\u2019, then add the reference by pressing \u2018Enter\u2019.<\/p>\n\n\n\n<p>4. Build the solution. If the build succeeded, the .dll file should be in your project\u2019s bin\\Debug folder. <\/p>\n\n\n\n<p>5. Once you have your .dll file, you can create a new Dynamics 365 Unified Operations project.<\/p>\n\n\n\n<p>6. Add a new Reference to your solution and find the previously created file in the Browse section.<\/p>\n\n\n\n<p>7. Create a new class and insert the following code: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>class ConsumeMNBWebService_DAX  \n{\n   str                 curr;  \n   int                 unit;  \n   real                value; <\/code>\n\n<code>   public static void main(Args _args)  \n   {\n      ConsumeMNBWebService_DAX  consumeMNBWebService = new ConsumeMNBWebService_DAX();      \n      consumeMNBWebService.getDataFromXML();  \n   } <\/code>\n\n<code>   public void getDataFromXML()  \n   {      \n      XmlDocument         doc = new XmlDocument(); \n      XmlNodeList         apiModelList; \n      XmlNodeListIterator iterator; \n      XmlElement          apiModel;\n      doc.loadXml(MNBWebService_DAX.MNBExchRates::GetExchRate()); \n      apiModelList = doc.getElementsByTagName('Rate'); \n      iterator = new XmlNodeListIterator(apiModelList); \n      while (iterator.moreValues()) \n      {     \n         apiModel = iterator.value();\n         unit = str2Int(apiModel.getAttribute('unit')); \n         curr = apiModel.getAttribute('curr'); \n         value = System.Decimal::Parse(apiModel.innerText(),\n         System.Globalization.CultureInfo::CreateSpecificCulture(\"hu-Hu\")); \n         if(unit &amp;&amp; curr &amp;&amp; value)\n         {     \n            info(strFmt(\"A line with Unit: %1, Currency: %2, Rate: %3 values have been inserted to MNBExchangeRates_DAX table\", unit, curr, value));    \n            this.insertDataToTable(); \n         } \n         iterator.nextValue();\n      } \n   }  <\/code>\n\n<code>   public void insertDataToTable()      \n   {\n      MNBExchangeRates_DAX exchangeRates;     \n      exchangeRates.RateUnit = unit;     \n      exchangeRates.CurrencyCode = curr;     \n      exchangeRates.ExchRate = value;     \n      xchangeRates.insert();  \n   }  \n} <\/code><\/pre>\n\n\n\n<p>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\u2019s attributes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">MNBCurrentExchangeRates\n   Day date=\"2020-01-06\"\n      Rate curr=\"AUD\" unit=\"1\" 205,4 \/Rate\n      .....\n      Rate curr=\"USD\" unit=\"1\" 294,81 \/Rate\n   \/Day\n\/MNBCurrentExchangeRates<\/pre>\n\n\n\n<p>The while loop continues until there are no more &#8220;Rate&#8221; sections.<br>It stores the \u2018unit\u2019, \u2018rate\u2019 and \u2018value\u2019 in variables. These variables are then inserted into the MNBExchangeRates_DAX table I created.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Possible improvement of code functionality <\/h3>\n\n\n\n<p> \u2022    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.<\/p>\n\n\n\n<p><\/p>\n\n\n<div class=\"taxonomy-post_tag wp-block-post-terms\"><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/msdyn365fo\/\" rel=\"tag\">#MSDyn365FO<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/consume-web-service\/\" rel=\"tag\">Consume web service<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/web-service-integration\/\" rel=\"tag\">Web service integration<\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>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-serviceIt 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 [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"1080","footnotes":""},"categories":[19],"tags":[25,91,93],"class_list":["post-660","post","type-post","status-publish","format-standard","hentry","category-dynamics-365fo","tag-msdyn365fo","tag-consume-web-service","tag-web-service-integration"],"_links":{"self":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts\/660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/comments?post=660"}],"version-history":[{"count":1,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts\/660\/revisions"}],"predecessor-version":[{"id":1388,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts\/660\/revisions\/1388"}],"wp:attachment":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/media?parent=660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/categories?post=660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/tags?post=660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}