How to use XMLPorts in Web Services (1)

15 Jan

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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XMLPortWSDemo.ExportDataWebService;

namespace XMLPortWSDemo
{
  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();
      service.ExportItems(ref navItems);

      GridView1.DataSource = navItems.NAVItem;
      GridView1.DataBind();
    }
  }
}

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.

13 thoughts on “How to use XMLPorts in Web Services (1)

  1. Thanks for this insight -very useful.

    Is it possible to extend this feature by passing parameters into the XMLPort from the c# calling code to control how the XMLport runs. E.g. passing in a USERID so the XMLPort can return a list of that users customers rather than the whole unfiltered list?

  2. Yes Chris, that is definitely possible. I will cover that in part 2 or 3.
    Sorry for my late reply, I didn’t notice your comment.

  3. Hi AJ,

    You write “UseDefaultNamespace and DefaultNamespace need to be edited before we can use the XMLPort in a web service”.
    Do you have some more background on why this is needed?

    b rg
    Luc

    • Hi Luc,

      Well, at least the UseDefaultNamespace must be set to Yes. If you don’t set this property, the wsdl will get show an error: “XmlPort , is not configured to use namespace. Please set UseDefaultNamespace property to Yes”. Obviously it is mandatory to specify a namespace in a wsdl.

      That still doesn’t answer the question why it is mandatory…

      The WSDL as an XML document. It’s root element is from the http://schemas.xmlsoap.org/wsdl/ namespace. All elements in an XML document that don’t have their own target namespace, have the default namespace from the parent element. When you use an XMLPort in a NAV web service, you introduce a new type in the XML document. To uniquely identify and to group these types, they must be marked with an attribute targetNamespace.

      For more information about this topic, see the next sites:
      http://msdn.microsoft.com/en-us/library/ms996486.aspx
      http://www.liquid-technologies.com/Tutorials/XmlSchemas/XsdTutorial_04.aspx

      Regards,
      Arend-Jan

  4. A small error in your text. You write when adding a Web Reference: “When using Visual Studio 2008 you may need to first choose ‘Add Service Reference’ > ‘Advanced’ > ‘Add Web Reference’”. I guess this should be VS 2010.

  5. Great Post! Instead of loading a DataGrid I would like to load the results into an XMLDocument so I can then read through it and do what I need.

    So intead of:
    GridView1.DataSource = navItems.NAVItem;

    I would like to load navItems.NAVItem into an XMLDocument varaible.

  6. Thanks for the article. You mentioned that
    “The UseDefaultNamespace and DefaultNamespace need to be edited before we can use the XMLPort in a web service:”

    Where may I find the XMLPort properties in NAV 2009 ?

    Regards

    • Hi John,

      As this is quite an old threat, I just read your question from a couple of weeks ago.
      You have not been creating many objects in NAV, have you?
      Go to the last empty line of the XML port (or any other object) and use the property button or Shift+F4 to go the properties of that object.

  7. Thanks for this very informative post. 2 questions:
    1) We want to use this method in Drupal (PHP) to receive NAV data, one direction only: from NAV to Drupal. Do you know how to call NAV from Drupal, or do you know the equivalent call for the Visual Studio Web Reference, in Drupal ? We have no Drupal experience, and our external web designer uses Drupal but does not know how to call NAV web service.
    2) If I understand well, there is no way to receive NAV data with a direct https call? A direct call only retrieves wdsl schema, no data.

    Regards,
    William

    • Unfortunately I don’t have any experience with Drupal.

      But regarding receiving NAV data with a direct https call I can tell you that is possible. You can use OData web services for that, assuming that you are using Dynamics NAV 2013 or higher.

Leave a Reply

Your email address will not be published.