Using XMLports With Web Services

Using XMLports With Web Services

  • Comments 11

As a follow-up on my recent webcast (found HERE), here is the general walkthrough of how to create an XMLport and use it for sending data to NAV.

First, what we want to do is create our XMLport and make sure it has the elements and values that we want.

XMLPort

For the root element, I have set maxOccurs = 1 to avoid any confusion.
For the general XMLport, the UseDefaultNamespace and the DefaultNamespace values have been edited as seen below.

SS02

Other than that, I have no code on my XMLport, but naturally, anything goes that would work on a regular XMLport. Now to the Codeunit:


ImportDim(VAR DimImport : XMLport DimImport) Return : Text[30]
DimImport.IMPORT;
EXIT('Import Run');

So basically we’re telling the XMLport to run an import and we’re returning to the Web Service that we’ve run. All we need to do now is expose the Web Service using Form 810:

SS03

Remember that the actual name of the codeunit does not have to match that of the service name here.

So now we move over to Visual Studio and start working with what we have. The first thing we’ll notice is that the WSDL matches our XMLport.

SS04

What we see is both the RootDimensions element which consists of multiple Dimension elements. From there, we can see the definition of the Dimension element the fields we’ve chosen to expose.

When creating a new project, we will go with a Windows Forms project this time.

SS05

And from there we will start off by adding a web reference to http://localhost:7047/DynamicsNAV/WS/Codeunit/DimensionImport .

The details on how to add a web reference can be found in the Developer and IT Pro Documentation.

On my new form, I have created two input boxes for the Code and Name of the dimension and a Create button.

SS06

And then we have the code on the Create button, along with helpful comments:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 

namespace
NAV2009SP1WSDemo

    using WSDI; 
    public partial class Form1 : Form 
    {    

 

 

 

        public Form1() 
       
            InitializeComponent(); 
       
 
        private void button1_Click(object sender, EventArgs e) 
       
            //Make sure the ResultLabel doesn't have any text on multiple runs 
            ResultLabel.Text = ""

           
//Create a WS reference instance, set credentials and define the company by specifying the URL. 
            DimensionImport NAVImport = new DimensionImport(); 
            NAVImport.UseDefaultCredentials = true
            NAVImport.Url = "http://localhost:7047/DynamicsNAV/WS/CRONUS%20International%20Ltd/Codeunit/DimensionImport"
            
            //First we create our root element 
            RootDimensions RootDim = new RootDimensions();

 

 

 

            //Then we create a List to handle our (possible) multiple dimensions 
            List<Dimension> DimList = new List<Dimension>();

 

 

 

            //And then we create a single dimension 
            Dimension Dim = new Dimension();  

 

            if (dimInputCode.Text != "" && dimInputName.Text != ""
           
                //We assign the values from our textboxes to the single dimension 
                Dim.DimensionCode = "AREA"
                Dim.Code = dimInputCode.Text; 
                Dim.Name = dimInputName.Text;

 

 

 

                //Then we add the single dimension to our list 
                DimList.Add(Dim);  

 

                //To get the list of dimensions "attached" to the root element, we use the following
                RootDim.Dimension = DimList.ToArray(); 
                try 
               
                    //Then we send to NAV and show our result 
                    ResultLabel.Text = NAVImport.ImportDim(ref RootDim);

 

 

 

               
                catch (Exception ex) 
               
                    //Show a possible exception 
                    ResultLabel.Text = ex.ToString(); 
               
           
            else 
           
                //Make sure there are values 
                ResultLabel.Text = "Both values must be filled"
           
        
    }
}

Our wonderful application is now ready to run and all we have to do is press F5:

SS07

We have now created our XMLport, exposed it using a codeunit and set data into it from a form based application.

Lars Thomsen

Microsoft Customer Service and Support (CSS) EMEA

Leave a Comment
  • Please add 7 and 1 and type the answer here:
  • Post
  • What about accessing this web service from a language like PHP? Can it be accessed directly using a SoapClient?

  • Perfect as ever cheers Lars!

  • I have the opposite problem with Web Services . How do i export a dataset from NAV using a web service ? My problem is : send a NAV a set of parametes ad a Codeunit that create a temporary table. Can i Export this temporary table ?

    thanks Maria

  • I great post, just one question I have remaining. I would like to save the xml data received. How would I go around doing this? For example:

    ImportDim(VAR DimImport : XMLport DimImport) Return : Text[30]

    //Here I would like to save the Xml content of the XML port, before executing it

    //

    DimImport.IMPORT;

    EXIT('Import Run');

    Thanks Pieter

  • I have the same question which Pieter asked earlier? How it is possible to save the data in NAV tables? I need to know how can we accomplish it using NAV code?

    Thanks

    KS

  • Don't Call the Import function yourself! The Webservice-Service does it by itself! If you call it by code, it gets executed twice!

    Same is for Export! As long as the xmlport is passed as a parameter (by var) the import/export is called by service tier. If you call it twice you have doubled the execution time and on import you can get an error that your data already exists!

    I don't know its a bug or a feature but this is important to know...

  • Hmmm...I'm trying to find a way to import xml data into NAV.  Using the method noted above, I would need to take my xml data and de-serialize it and than put it into lists, right?  Is it just me, or does that not make any sense at all?  I come from the GP world.  Of there, we can simlple have off a XML document to a webservice or to econnect, and the data is validated and imported.  There is nothing like that in NAV?  Take xml data, de-serialize it, and then push it through a xmlport??? Seems like a long way home...

  • A few slow answers:

    Temporary data cannot be exported as far as I know.

    The data cannot be saved before running the dataport. This is a feature request, but I doubt it's something we'll see. The only alternative is to use BigText instead, which will give you a far worse structure in the WSDL.

    Rene - I'm a little confused. On which side do you mean you have to de-serialize? NAV or C#? I recommend you go here:

    msdn.microsoft.com/.../dd355339.aspx

    And check the two walkthroughs labelled "Walkthrough: Creating and Consuming a Codeunit Web Service" and "Walkthrough: Registering and Consuming a Page Web Service". That will show you the alternatives.

  • How would you go about sending data from NAV using an XMLPort and receiving it using codeunit WS as in this example in another NAV instance? I have used codeunit WS with DotNet wrapper to have NAV communicate with another NAV so far with success but I am unsure what the code to send an XMLPort to codeunit web service would look like.

    Help would be greatly appreciated.

  • Hello!

    Sorry for my English.

    How can I import exoprt data through a web service between the two servers Navision? As

    Thank you!

  • Here, ResultLabel.Text = NAVImport.ImportDim(ref RootDim);

    what does that ref RootDim mean?? since I am getting an error over there like invalid argument.

    Kindly help..............

Page 1 of 1 (11 items)