This walkthrough demonstrates how to use file functions to send a file from a client to the server, use stream functions to read the file, and use an XMLport to load values into the database. Business logic in Microsoft Dynamics NAV runs on Microsoft Dynamics NAV Server instead of on the client. As a result, any business logic that reads data from a file needs to first move a copy of that file from the client to the server.
About This Walkthrough
This walkthrough illustrates the following tasks:
- Creating an XML file that contains two
elements with values.
- Creating a table in Microsoft Dynamics NAV to
hold the data from the XML file.
- Creating a codeunit that does the
- Sends the file from a user-specified location
to a location on the server
- Reads the data
- Writes the data to the table
- Deletes the file from the server
- Sends the file from a user-specified location to a location on the server
- Associating the codeunit with an action on a
Role Center page.
- Testing the codeunit.
To complete this walkthrough, you will need:
- Microsoft Dynamics NAV installed with a
Creating an XML File
The first step is to create a simple XML file that contains the data that the codeunit will read into the table. This represents the file on a client computer.
To create the XML file
Enter the following code.
<?xml version="1.0" encoding="UTF-8"?><head><valueI>10</valueI><valueII>11</valueII></head>
Save the file in the root of the My Documents directory. Name the file simple.xml.
Open the file in Internet Explorer to ensure that the XML is well formed.
Creating a Table
The next step is to create the table that will hold the data that is read from the XML file.
To create the table
In Object Designer, choose Table, and then choose New.
Table Designer opens.
Choose Field Name in the first row and then enter IntA. Set Data Type to Integer.
Choose Field Name in the second row and then enter IntB. Set Data Type to Integer.
Save the table, giving it a name of TestTable and an ID of 50010.
Close Table Designer.
Creating the Codeunit
The next step is to create a codeunit that will prompt the user to specify the location of the XML file, load the XML file into a stream, and then write the values of those variables to the table using an XMLport.
To create the codeunit
In Object Designer, choose Codeunit, and then choose New.
C/AL Editor opens.
Choose the OnRun() function in the C/AL Editor.
Choose View and then choose C/AL Locals.
The C/AL Locals window opens.
Choose the Variables tab.
Enter the following local variables.
Name DataType Length
Choose the next empty line to ensure that the local variables are saved.
Close the C/AL Locals window.
Enter the following code in the C/AL Editor for the OnRun function.
BEGIN IF UPLOADINTOSTREAM( 'Select the simple.xml file', 'c:\temp', 'XML File *.xml| *.xml', ClientFileName, TestStream) THEN MESSAGE('File Successfully Uploaded') ELSE EXIT; XMLPORT.IMPORT(50010,TestStream); END;
Save the codeunit, giving it a name of UploadFile and an ID of 50010.
Close the code editor.
Creating the XMLport
The next step is to create an XMLport.
To create the XMLport
In Object Designer, choose XMLport, and then choose New.
XMLport Designer opens.
Enter the following information.
Node Name Node Type Source Type Data Source
Indent the valueI and valueII elements one level by choosing the right arrow.
Save the XMLport, giving it a name of UploadFile and an ID of 50010.
Close XMLport Designer.
Testing the Codeunit
The final step is to test the codeunit to ensure that it correctly adds the values from the simple.xml file to the TestTable. To test the codeunit, you will run it from Object Designer.
To test the codeunit
In Object Designer, choose Codeunit, select codeunit 50010 UploadFile, and then choose the Run button.
The Select the simple.xml file window opens.
Browse to the location of simple.xml and then choose Open.
The message File Successfully Uploaded appears.
Open the development environment.
In Object Designer, choose Table, choose 50010 Test Table, and then choose Run.
The TestTable-Table window opens with the values of 10 and 11 in the IntA and IntB columns, respectively.