Friday, November 23, 2012
As many developers know, Pages and Codeunits can be exposed as Web Services. When using a Codeunit as a Web Service we ordinarily use simple data types as function parameters. It is also possible to pass XMLPorts.
Let's view an example and test it. You need to create an XMLPort that will be used as our XML document. In our example, we will use the Currency table. First of all, create the data model for the XMLPort and add the elements you want to be seen in the XML document passed through the web service. Note that if some elements are optional, the MinOccurs property should be set to Zero. Otherwise if those elements are not filled in with values using the Web Service, they will get Null value and the Web Service will cause an exception.
Last but not least, the XMLPort property is set from "UseDefaultNamespace" to "Yes" in order to get the right XML structure.
Let's go further. Create a sample Codeunit with a function Currencies(VAR XML : XMLport TESTXMLPORT) with the earlier created XMLPort as the parameter. Note that the parameter has to be Variable.
Now you can write any filters you want. If you do not use any filters or leave this function blank, all the currencies will be accessible through the Web Service.
If you want to enable insertion into the Currency table, add XML.IMPORT; in the function's code. Note that the same written function will be used for both reading and writing to the web service. Save the codeunit and expose it as a web service.
Now we are going to test it using Microsoft Visual Studio. Create a new Windows Form Application. Add a Web Reference to http://localhost:7047/DynamicsNAV/WS/TESTCOMPANY/Codeunit/TestXMLPort. Add a textbox and a button to the form and double-click the button. Add the following code to the button's on-click event.
And finally, compile and test the application.
To read more about Microsoft Dynamics Web Services with XMLPorts, just click on the links library below. Happy reading:)