About two weeks ago I posted an explanation why Pages are not always sufficient for use with web services. Using XMLPorts in web services offer powerful possibilities that are not available with Pages.
In this post I will explain the basics of using XMLPorts in a web service. Upcoming posts will also cover more advanced features.
Here is a simple XMLPort that we want to use in a web service:
If we use this XMLPort in a web service without further configuring it, we will get the next error message:
The UseDefaultNamespace and DefaultNamespace need to be edited before we can use the XMLPort in a web service:
To use this XMLPort in a web service we need to create a Codeunit with a function that uses this XMLPort as a parameter.
Next we publish this Codeunit as a web service, using Form 810:
No we can check the WSDL using Internet Explorer.
Let’s switch over to Visual Studio. We are creating a web application that displays the exported item list.
First we need to add a Web Reference. (When using Visual Studio 2010 you may need to first choose ‘Add Service Reference’ à ‘Advanced’ à ‘Add Web Reference’).
Next, we add a simple GridView to the web form and let it automatically generate the columns.
And here is the code to consume the webservice and bind the result to the GridView.
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
ExportData service = new ExportData();
service.UseDefaultCredentials = true;
service.Url = "http://localhost:7047/DynamicsNAV/WS/CRONUS%20International%20Ltd/Codeunit/ExportData";
NAVItems navItems = new NAVItems();
GridView1.DataSource = navItems.NAVItem;
That’s all folks! This little application produces this output:
Did you notice that the Codeunit has no code at all? Isn’t that beautiful?
In next posts I will try to:
- Explain why NAV automatically exports the data without any line of C/AL code and how to influence this behavior.
- Show how to filter the data within NAV before exporting it.
- Implement paging.