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. In Microsoft Dynamics NAV 2009, business logic 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 following:
-
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
-
-
Associating the codeunit with an action on a Role Center page.
-
Testing the codeunit.
Prerequisites
To complete this walkthrough, you will need:
-
Microsoft Dynamics NAV 2009 installed with a developer license
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
-
Open Notepad.
-
Enter the following code.
Copy 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, click Table, and then click New.
Table Designer opens.
-
Click Field Name in the first row and then enter IntA. Set Data Type to Integer.
-
Click 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, click Codeunit, and then click New.
C/AL Editor opens.
-
Click the OnRun() function in the C/AL Editor.
-
Click View and then click C/AL Locals.
The C/AL Locals window opens.
-
Click the Variables tab.
-
Enter the following local variables.
Name DataType Length TestFile
File
TestStream
InStream
NVOutStream
OutStream
ClientFileName
Text
1024
-
Click 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.
Copy Code BEGIN IF ISSERVICETIER THEN BEGIN IF UPLOADINTOSTREAM( 'Select the simple.xml file', 'c:\temp', 'XML File *.xml| *.xml', ClientFileName, TestStream) THEN MESSAGE('File Successfully Uploaded') ELSE EXIT; END ELSE BEGIN TestFile.OPEN('C:\temp\simple.xml'); TestFile.CREATEINSTREAM(TestStream); END; XMLPORT.IMPORT(50010,TestStream); IF NOT ISSERVICETIER THEN TestFile.CLOSE; 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, click XMLport, and then click New.
XMLport Designer opens.
-
Enter the following information.
Node Name Node Type Source Type Data Source head
Element
Table
<TestTable>(TestTable)
valueI
Element
Field
<TestTable>::IntA
valueII
Element
Field
<TestTable>::IntB
-
Set the Indentation property for value I and value II to 1.
-
Save the XMLport, giving it a name of UploadFile and an ID of 50010.
-
Close XMLport Designer.
Associating the Codeunit with an Action
Now that you have the completed codeunit, you need to associate it with an action in the RoleTailored client so it can be tested. In this case, you will add a new action to the Sales Order Processor role center and name it Upload File to Server. When selected, this action will call the codeunit.
To associate the codeunit with an action
-
In Object Designer, click Page, select page 9006 Order Processor Role Center, and then click Design.
Page Designer opens.
-
Click the first empty line on the page, click View, and then click Actions.
Action Designer opens.
-
Click the first empty line in Action Designer and in the Caption column for that line, enter Upload File to Server. Set the Type to Action.
-
With the Upload File to Server line selected, press SHIFT+F4 to open the Properties window.
-
Set the RunObject property value to c50010. When this action is selected on the role center, codeunit 50010 will run.
-
Close the Properties window.
-
Close Action Designer.
-
Close Page 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
-
Open the RoleTailored client.
-
On the Sales Order Processor role center, click Actions, and then click UploadFileToServer.
The Select the simple.xml file window opens.
-
Browse to the location of simple.xml and then click Open.
-
Click OK.
-
Open the Classic client.
-
In Object Designer, click Table, click 50010 Test Table, and then click Run.
The TestTable-Table window opens with the values of 10 and 11 in the IntA and IntB columns, respectively.
See Also
© 2010 Microsoft Corporation. All rights reserved.