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!
2. Add a new Service Reference to the References node of your project and insert the URL of the web service.
• 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.
public static string GetExchRate()
{
var binding = new System.ServiceModel.BasicHttpBinding();
var endpointAddress = new EndpointAddress("http://www.mnb.hu/arfolyamok.asmx");
MNBExchRateReference.MNBArfolyamServiceSoapClient client = new MNBExchRateReference.MNBArfolyamServiceSoapClient(binding, endpointAddress);
MNBExchRateReference.GetCurrentExchangeRatesRequestBody request = new MNBExchRateReference.GetCurrentExchangeRatesRequestBody();
client.Close();
return Convert.ToString(client.GetCurrentExchangeRates(request).GetCurrentExchangeRatesResult);
}
• 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:
class ConsumeMNBWebService_DAX { str curr; int unit; real value;
public static void main(Args _args) { ConsumeMNBWebService_DAX consumeMNBWebService = new ConsumeMNBWebService_DAX(); consumeMNBWebService.getDataFromXML(); }
public void getDataFromXML() { XmlDocument doc = new XmlDocument(); XmlNodeList apiModelList; XmlNodeListIterator iterator; XmlElement apiModel; doc.loadXml(MNBWebService_DAX.MNBExchRates::GetExchRate()); apiModelList = doc.getElementsByTagName('Rate'); iterator = new XmlNodeListIterator(apiModelList); while (iterator.moreValues()) { apiModel = iterator.value(); unit = str2Int(apiModel.getAttribute('unit')); curr = apiModel.getAttribute('curr'); value = System.Decimal::Parse(apiModel.innerText(), System.Globalization.CultureInfo::CreateSpecificCulture("hu-Hu")); if(unit && curr && value) { info(strFmt("A line with Unit: %1, Currency: %2, Rate: %3 values have been inserted to MNBExchangeRates_DAX table", unit, curr, value)); this.insertDataToTable(); } iterator.nextValue(); } }
public void insertDataToTable() { MNBExchangeRates_DAX exchangeRates; exchangeRates.RateUnit = unit; exchangeRates.CurrencyCode = curr; exchangeRates.ExchRate = value; xchangeRates.insert(); } }
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.
MNBCurrentExchangeRates Day date="2020-01-06" Rate curr="AUD" unit="1" 205,4 /Rate ..... Rate curr="USD" unit="1" 294,81 /Rate /Day /MNBCurrentExchangeRates
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.