Microsoft Dynamics NAV: XMLPort through BigText

With the addition of Web Services, exchanging data and processing code with Microsoft Dynamics NAV from an external source became a relatively simple task.  By exposing an XMLPort, working with class object and binding to the underlying data allows for an easy way to exchange data with Microsoft Dynamics NAV.  If the requirement is to pass the text (string) output of the XMLPort as text you can pass it through as a BigText variable.

// Get Recordset
Customer.GET(‘30000’);

//Set the view for the XMLPort
CustomerXML.SETTABLEVIEW(Customer);

// Use the TempBlob record to get a blob stream
TempBlob.Blob.CREATEOUTSTREAM(OS);
CustomerXML.SETDESTINATION(OS);
// export the contents of the XMLPort to a blob
CustomerXML.EXPORT;

TempBlob.CALCFIELDS(Blob);
TempBlob.Blob.CREATEINSTREAM(IS);
// Read the blob contents to the big text
Response.READ(IS);

 

Screenshot of XMLPort to BigText Microsoft Dynamics NAV

The variables are declared as follows:

Microsoft Dynamics NAV variables for sample code

Microsoft Dynamics Nav: Using an XMLPort as a .NET DataSource

I previously discussed connecting to Microsoft Dynamics Nav using a Web Service and also discussed using an XMLPort to create an XML file. Building upon these concepts we can use a Microsoft Dynamics Nav XMLPort as a DataSource for a .NET application. I recommend reading the previous posts before continuing with this one.

Microsoft Dynamics Nav: Using an XMLPort as a .NET DataSource

This example slightly expands on the previous example and uses the Customer XMLPort as the datasource.

  1. Ensure that the MaxOccurance property is set for the Text element Open_Balance.
    XMLPort Designer
  2. Create a CodeUnit with a Function that uses the XMLPort as a parameter. Ensure that the parameter is marked as a Var parameter.
    CodeUnit Function
  3. Publish the CodeUnit as a Web Service from within Microsoft Dynamics Nav and restart the “Microsoft Dynamics Nav Web Services” service.
    Microsoft Dynamics Nav Web Service
  4. Create a new .NET Application and add the Web Reference for the CustomerExport CodeUnit published as a Web Service
  5. Place a DataGridView on the form
  6. Set the DataSource for the DataGridView to be the Microsoft Dynamics Nav XMLPort
    private void Form1_Load(object sender, EventArgs e)
            {
                CustomerExport_Binding ws = new CustomerExport_Binding();
                ws.UseDefaultCredentials = true;
                ws.Url = "http://localhost:7047/DynamicsNAV/WS/CRONUS%20USA,%20Inc./Codeunit/CustomerExport";
    
                Customers customers = new Customers();
                ws.ExportCustomer(ref customers);
                
                BindingSource bs = new BindingSource();
                dataGridView1.DataSource = bs;
    
                bs.DataSource = customers.Customer;
            }
    

A Microsoft Dynamics Nav XMLPort can easily also be the datasource for a DataGrid in an ASP.NET application.

The sample application referenced in this post can be downloaded >>>here<<<.

Microsft Dynamics Nav: Export XMLPort to File

A Microsft Dynamics Nav XMLPort is conceptually similar to a Dataport and can be used to create an XML format file to exchange information between systems. The follow example demonstrates exporting customer data to an XML file using Microsoft Dynamics Nav.

  1. Using the XMLPort Designer create an XMLPort that contains the data that you would like to Export
    Microsoft Dynamics Nav XMLPort
  2. If an Element is of type Text the value can be specified in code in the Export::OnBeforePassVariable trigger
    Microsoft Dynamics Nav XMLPort OnBeforePassVariable Trigger
  3. Create the code to export the data to an XML file
    1. Filter the RecordSet for the data desired
    2. Create a File
    3. Create an OutStream
    4. Set the view of the XMLPort to be the filtered RecordSet
    5. Export the XMLPort data (to the OutStream)
    6. Close the XMLFile
      Microsoft Dynamics Nav XMLPort Code

The sample application referenced in this post can be downloaded >>>here<<<.

 

Microsoft Dynamics Nav: .NET Interoperability

Microsoft Dynamics Interoperability with .NET

With Microsoft Dynamics Nav 2009 you can take advantage of .NET Framework interoperability and create code that allows Microsoft Dynamics NAV objects and .NET Framework objects to interact. This interoperability can be leveraged to execute Microsoft Dynamics Nav business logic from a .NET application. Here is an example of how to create a .NET Application that executes Nav code within a CodeUnit through the use of the “Microsoft Dynamics NAV Business Web Services”.

Before creating any code it is important that Microsoft Dynamics Nav is properly setup and configured to work with Web Services.

  1. Edit the CustomSettings.config in the Microsoft Dynamics Nav Service directory to work with your database and port.
    Microsoft Dynamics Interoperability with .NET
  2. Ensure that the “Microsoft Dynamics NAV Business Web Services” and Microsoft Dynamics Nav Server” are configured to use an account that has access to the database
    Microsoft Dynamics Interoperability with .NET
  3. In Microsoft Dynamics Nav create a CodeUnit with two functions that will be executed from the .NET Application
    Microsoft Dynamics Interoperability with .NET
  4. Publish the CodeUnit created in step 3 as Web Service and restart the “Microsoft Dynamics NAV Business Web Services” service. From the Classic Client fill out the appropriate data in the form.
    Microsoft Dynamics Interoperability with .NET
  5. Create a new .NET Application
  6. Add Microsoft Dynamics Nav as Web Service Reference to the project
    1. Right Click References
    2. Click Add Web Reference buttonClick Advanced buttonAdd Reference
    3. Enter the Web Service URL for your Microsoft Dynamics Nav installation
    4. Enter your Web Reference Name

    Microsoft Dynamics Interoperability with .NET

  7. Create an object to communicate with your service
    namespace BP.NavWebService
    {
        public class NavWebService
        {
            // when registering the web reference connect to the server with an address like
             //http://localhost:7047/DynamicsNAV/WS/CRONUS%20USA,%20Inc./services
    
            private Web1_Binding ws;
            public Web1_Binding Ws { get { return ws; } }
    
            public NavWebService(string url, bool usedefaultcredentials)
            {
                ws = new Web1_Binding();
                // Use default credentials for authenticating against Microsoft Dynamics NAV.
                ws.UseDefaultCredentials = usedefaultcredentials;
                ws.Url = url;
            }
        }
    }
    
  8. Interact with your service
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            NavWebService Nws;
    
            private void button1_Click(object sender, EventArgs e)
            {
                textBox2.Text = Nws.Ws.GetCustomerName(textBox1.Text);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Nws.Ws.SetCustomerName(textBox1.Text, textBox2.Text);
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                string Url = "http://localhost:7047/DynamicsNAV/WS/CRONUS%20USA,%20Inc./Codeunit/Web1";
                Nws = new NavWebService(Url, true);
            }
        }
    

 

The sample application referenced in this post can be downloaded >>>here<<<.