OBJECT Codeunit 50051 Web Service SOAP example { OBJECT-PROPERTIES { Date=04/09/18; Time=17:13:56; Modified=Yes; Version List=SOAP; } PROPERTIES { OnRun=BEGIN xml := '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>'+ '<GetHolidaysForYear xmlns="http://www.holidaywebservice.com/HolidayService_v2/">'+ '<countryCode>UnitedStates</countryCode>'+ '<year>2019</year>'+ '</GetHolidaysForYear>'+ '</soap:Body></soap:Envelope>'; url := 'http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx'; uriObj := uriObj.Uri(url); Request := Request.CreateDefault(uriObj); Request.Method := 'POST'; Request.ContentType := 'text/xml'; soapAction := '"http://www.holidaywebservice.com/HolidayService_v2/GetHolidaysForYear"'; Request.Headers.Add('SOAPAction', soapAction); Request.Timeout := 120000; // Send the request to the webservice stream := stream.StreamWriter(Request.GetRequestStream(), ascii.UTF8); stream.Write(xml); stream.Close(); // Get the response Response := Request.GetResponse(); reader := reader.XmlTextReader(Response.GetResponseStream()); // Save the response to a XML document := document.XmlDocument(); document.Load(reader); FileSrv := FileMgt.ServerTempFileName('xml'); document.Save(FileSrv); // Get from the server ToFile := FileMgt.ClientTempFileName('xml'); FileMgt.DownloadToFile(FileSrv, ToFile); // Show the response XML HYPERLINK(ToFile); END; } CODE { VAR uriObj@1000000001 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Uri"; Request@1000000002 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest"; stream@1000000003 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamWriter"; Response@1000000004 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse"; reader@1000000006 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlTextReader"; document@1000000007 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument"; ascii@1000000008 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.Encoding"; FileMgt@1000000014 : Codeunit 419; FileSrv@1000000015 : Text; ToFile@1000000016 : Text; xml@1000000010 : Text; url@1000000005 : Text; soapAction@1000000000 : Text; BEGIN END. } }
This is an example of a call to a Webservice who is expecting 2 parameters, countryCode and yearOBJECT Codeunit 50051 Web Service SOAP example { OBJECT-PROPERTIES { Date=04/09/18; Time=17:13:56; Modified=Yes; Version List=SOAP; } PROPERTIES { OnRun=BEGIN xml := '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>'+ '<GetHolidaysForYear xmlns="http://www.holidaywebservice.com/HolidayService_v2/">'+ '<countryCode>UnitedStates</countryCode>'+ '<year>2019</year>'+ '</GetHolidaysForYear>'+ '</soap:Body></soap:Envelope>'; url := 'http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx'; uriObj := uriObj.Uri(url); Request := Request.CreateDefault(uriObj); Request.Method := 'POST'; Request.ContentType := 'text/xml'; soapAction := '"http://www.holidaywebservice.com/HolidayService_v2/GetHolidaysForYear"'; Request.Headers.Add('SOAPAction', soapAction); Request.Timeout := 120000; // Send the request to the webservice stream := stream.StreamWriter(Request.GetRequestStream(), ascii.UTF8); stream.Write(xml); stream.Close(); // Get the response Response := Request.GetResponse(); reader := reader.XmlTextReader(Response.GetResponseStream()); // Save the response to a XML document := document.XmlDocument(); document.Load(reader); FileSrv := FileMgt.ServerTempFileName('xml'); document.Save(FileSrv); // Get from the server ToFile := FileMgt.ClientTempFileName('xml'); FileMgt.DownloadToFile(FileSrv, ToFile); // Show the response XML HYPERLINK(ToFile); END; } CODE { VAR uriObj@1000000001 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Uri"; Request@1000000002 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest"; stream@1000000003 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamWriter"; Response@1000000004 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse"; reader@1000000006 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlTextReader"; document@1000000007 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument"; ascii@1000000008 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.Encoding"; FileMgt@1000000014 : Codeunit 419; FileSrv@1000000015 : Text; ToFile@1000000016 : Text; xml@1000000010 : Text; url@1000000005 : Text; soapAction@1000000000 : Text; BEGIN END. } }
Is possible that to call your web service you only need to change the parts in red, but you must see the webservice documentation.
Regards
Answers
https://community.dynamics.com/nav/b/moxie4nav/archive/2015/09/10/call-external-web-service-using-new-cu-1290
vjeko.com/web-services-black-belt-consuming-nav-web-services-using-pure-cal/
Hi ErictP,
Thanks a lot for your reply.
I tried to put the code provided in your first link in a report and try running it, so that I may have a start.
But I received the following error.
I tried to google on the error, but couldn't find any solution. Can you help?
Is possible that to call your web service you only need to change the parts in red, but you must see the webservice documentation.
Regards
Thanks ftornero,
I would try the above solution and inform.
Thanks a lot ftornero,
your solution really helped me.
Thanks so much.
After you get the response XML file you can save it to a OutStream and later on use as a Instream for the SETSOURCE method in the XMLPort.
Regards.
Thank you for your reply
I have a question about the format of the XML file after getting from the web service. I will set up XMLPort file how to get data into NAV 2013 table.
Thanks so much.
If your XML looks like the last one this XMLPort will import the data into a NAV table.
You maybe need to test the MsgError->Code before do anything else.
Regards.
https://rockwithnav.wordpress.com/2016/02/23/consume-net-webservice-dotnet-data-type/
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/
I would like to ask more questions about variable transmission.
Example: I want to call the Webservice from day to day, so what would it look like. And I can retrieve data from the current date back before 30 days.
How do I call the parameter?
Thanks so much
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/
I meant to call parameters from the Service. In the example above.
But here I want to call the order date parameter (from day to day)
Thanks.
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/
You need to check the web service WSDL to get the functions that you can use and how use them.
With a program like SoapUI and this WSDL you can get an example of how to call to those functions.
Regards.
Is there any problem when I call the service with the port?
Ex : http://www.holidaywebservice.com:9000
Then function change?
If yes, do I need to make any changes to the code.
Thanks so much
If the call to the web service is defined with a port number, there is no problem using it.
Regards.
Hi Everybody.
When I run the manual from CodeUnit, then it's work.
But when using the job queue, the following error:
Microsoft Dynamics NAV Application Server attempted to issue a client callback to create a DotNet object: C:\ProgramData\Microsoft\Microsoft Dynamics NAV\70\Server\MicrosoftDynamicsNavServer$NAV70\users\JOMY\***\TEMP\__TEMP__8c48ec2d48b04f9da1719be1f781f61a.tmp.txt (CodeUnit 419 File Management). Client callbacks are not supported on Microsoft Dynamics NAV Application Server.
Please fix help.
Thanks so much
In the job queue your code is running in the server, so any DotNet var must run on the server.
The error that you get is because you are using a function inside the codeunit 419 that is trying to manage same file in the client side, you need to change that function to the eqivalent one running in the sever side.
Regards
Problem soved. I'm have fix on line