Professional Documents
Culture Documents
Introduction
Dataports and XMLports are objects that are used for importing data from and
exporting data to external files.
Dataports are used to export data from Microsoft Dynamics® NAV to external
text files, and to import data from external text files into Microsoft Dynamics
NAV. Importing and exporting data is useful when data is collected outside
Microsoft Dynamics NAV and must be incorporated into the database, or when it
must be distributed from the Microsoft Dynamics NAV database to an external
location.
XMLports are used to export and import data to and from external XML
document files. These are similar to dataports, only the XMLport encapsulates
the data in XML format. This lets developers exchange information between
different computer systems in a streamlined way. In Microsoft Dynamics NAV
2009, XMLport is also used to import and export data to and from external text
files, when using the RoleTailored client.
Dataport Fundamentals
Dataports are used to export and import data to and from external text files.
Dataports can be defined to specifically be used for import or export data, or
both. If the dataport is used for both import and export, only on execution does
the dataport determine whether the process is an import or an export and the
name of the file to read from or to write to.
There are several options for the format of the external file which includes fixed
and variable formats.
The following shows components of a dataport and how they are related.
Dataports are created and designed in the Dataport Designer which is accessed
from the Object Designer.
Properties
A property is an attribute of an object, or its component, that characterizes and
specifies behavior of the parent in some ways, such as length and position of a
dataport field in a line during import.
The dataport description contains properties that are related to the dataport itself
and properties that are related to the other components of the dataport, such as
data items, dataport fields or request forms.
Triggers
Certain predefined events that occur to a dataport cause the system to execute a
user-definable C/AL function. The event and the function together are called a
trigger.
• Dataport triggers
• Data item triggers
• Dataport field triggers
• Request form triggers
Data Items
The data model of a dataport is built from data items. A data item corresponds to
a table. To retrieve information from tables in the database, define data items.
Data items in a dataport cannot be indented. They have to be processed one at a
time.
Dataport Fields
A dataport field refers to a field in an external file from which data is to be
imported. Dataport fields in the external file are defined either as having a fixed
length, or as delimited by certain characters.
Dataport fields are specified in the Field Designer which can be opened from the
Dataport Designer.
Request Form
The request form is the form that is run before the actual dataport begins
execution. It is used to collect requests and options from the user of the dataport
of things such as the name of the external file.
Design Dataports
Designing a dataport involves several tasks. They are as follows:
These tasks consist primarily of setting various properties in the dataport objects,
such as dataport properties, data item properties and dataport field properties.
Data Model
The data model is designed by designing data items. A data item corresponds to a
table.
When a dataport is used to export data, each data item in the dataport is iterated
for all records in the underlying table. Sorting order, keys and table views can be
set accordingly. Developers can decide whether the result is written to the
external file or not.
When a dataport is used to import data, records read from the external file are
inserted to the table that corresponds to the data item. Developers can examine
the records before inserting them, specify whether records are inserted
automatically and decide whether records already in the database are overwritten
or updated when a record with the same primary key is read from the external
file. Developers may also determine to omit this record.
External File
The layout of the external file is defined by setting properties which include
setting dataport properties and dataport field properties.
When a dataport is used to export data, these properties describe how the dataport
fields and records must be written to the file.
When a dataport is used to import data, these properties describe how the input
stream must be broken up into records and dataport fields.
Dataport Properties
Dataport properties describe the dataport in general. Several properties, such as
the Import and FileName properties, can be set and reset dynamically. For
example, developers can create a dataport where the user can do one of the
following:
The FileFormat property determines the format of the external file. In other
words, it determines the dataport fields' behavior in a record. The following list
shows the options available for the FileFormat property and the dataport fields'
behavior for each option.
FileFormat Remarks
Property
Value
Fixed The dataport fields in a record have a fixed width. Developers
define the starting position and the width of each dataport
field in the record in the Dataport Field Designer.
Variable The dataport fields in a record have varying widths.
Developers define the characters that separate the dataport
fields in the FieldSeparator property.
Property Remarks
AutoSave This property determines whether imported records are
automatically inserted in a C/SIDE table.
AutoUpdate This property determines whether imported records are
initialized with values from an existing record with the same
primary key.
AutoReplace This property determines whether imported records
automatically replace existing records with the same primary
key.
These three properties determine how records that are read from the external file
are handled. They are also used to resolve the conflict that arises when a record
that is read from the external file during import has the same primary key as a
record that already exists in the database table. The following table shows
combinations of these properties values and their result.
• When the prices are calculated and the developer is ready to import
the file that has the new prices, it is obvious that the records read
from the external file have the same primary key as records that are
already in the database.
• Using AutoSave and AutoReplace does not resolve this problem. If
the developer is replacing every record with the corresponding
record from the import file, all the information except the item
numbers and the prices are lost, assuming that the table contains
information other than the item numbers and the prices, such as
name and description of the items.
• AutoUpdate solves this dilemma. When a record is imported, it
actually replaces the existing record, but fields that are not present in
the imported record are initialized with the data from the already
existing record instead of being left empty. The existing record is
updated with the revised information.
During import, when a value is too large for the data type or the defined width of
the database table field where it is to be inserted, an error occurs and stops the
execution. Because the whole dataport is inside a transaction, no traces are left of
this aborted run in the database.
During export, data is converted to text before the export. When the Width
property is smaller than the actual width of the data after conversion, the contents
are truncated from the right until the defined Width. A number is not rounded or
truncated as a number, but as text, from the right.
Developers receive an error at design time if they defined dataport fields that
have starting positions and widths that cause these dataport fields to overlap. This
error prevents compilation of the dataport. However, it is possible to have gaps
between dataport fields. The starting position of the next dataport field does not
have to be exactly where the previous dataport field ended. These gaps may skip
unnecessary information or create blank columns in the file.
If the Field Menu is used to add dataport fields in the Field Designer, the Width
property is set to several default values, depending on the field data type. The
following table shows the default values for the Width property for each field
data type.
Usually, dataports are run from the Navigation Pane in the Classic client, or from
command buttons in a form, also in the Classic client. The RoleTailored client
does not support dataport objects.
Dataports can also be run from the Object Designer or directly from the Dataport
Designer. If the dataport is run directly from the Dataport Designer and the
dataport is used for import process, no records are actually saved in the database
table.
1. In the Object Designer's Dataport List, click the New button. The
Dataport Designer opens.
2. Open the Properties window for the dataport and set the following
properties:
o Import: No (This specifies the dataport to be used only for
export.)
o FileFormat: Fixed (This specifies the external file to have fixed
format.)
NOTE: Instead of typing the fields manually, use the Field Menu to add multiple
fields from the table to the Field Designer.
7. Close the Field Designer and open the Properties window for the G/L
Account data item, and set the following property:
o CalcFields: Balance at Date,Net Change
NOTE: Instead of typing the value directly to the CalcFields property, click the
Assist-Edit button on the CalcFields property to open the Field List window. Add
the fields in the Field List window and then click OK.
9. Compile and save the dataport by clicking File, Save As. The Save
As dialog box opens.
10. Type 90000 in the ID field and G/L Account Export Fixed in the
Name field, ensure that the Compiled check box is selected, and
then click OK. This compiles and saves the dataport.
11. Close the dataport.
• Removing the first tab in the request form so that users cannot set
filters and keys.
• Setting the dataport to export only accounts where the Account Type
is Posting or End-Total.
• Formatting the numbers as thousands. There must be no thousand
separators, no decimals, and the sign must be prefixed.
To remove the tabs that correspond to the data items in the request form, clear the
ReqFilterFields property and set a sorting key in the DataItemTableView
property of the data items.
The following steps show how to remove the G/L Account tab from the request
form.
1. Design dataport 90000, G/L Account Export Fixed from the Object
Designer.
2. Open the Properties window for the G/L Account data item and set
the DataItemTableView property to SORTING(No.)
3. Close the Properties window and then compile, save and close the
dataport.
4. Run the dataport. Notice the request form only shows one tab, the
Options tab.
1. Design dataport 90000, G/L Account Export Fixed from the Object
Designer.
2. Open the Properties window for the G/L Account data item.
3. Click the Assist-Edit button on the DataItemTableView property to
open the Table View window.
4. Click the Assist-Edit button on the Table Filter field to open the
Table Filter window, and type the following:
This creates a table filter that selects records where Account Type is Posting or
End-Total (the character between the two values is a | (pipe) which means OR).
1. Design dataport 90000, G/L Account Export Fixed from the Object
Designer.
2. With the G/L Account data item selected, click View, Dataport
Fields. The Field Designer opens.
3. Open the Properties window for the Balance at Date field, and set
the following property:
o SourceExpr: FORMAT(ROUND("Balance at
Date"/1000,1,'='),0,1)
In this expression, the Balance at Date field is first divided by 1000. The result
is rounded by the ROUND function, and then the FORMAT function is used to
display the return value from the ROUND function in format 1 (which for a
decimal value means <Sign><Integer><Decimals>).
4. Open the Properties window for the Net Change field, and set the
following property:
o SourceExpr: ROUND("Net Change"/1000,1,'=')
o Format: <Sign><Integer><Decimals>
NOTE: This achieves the same effect as setting the SourceExpr property with
FORMAT function, as in the Balance at Date field.
5. Close the Properties window, and then compile, save and close the
dataport.
6. Run the dataport, save the external file as GLAccountFixed3.txt and
view the text file.
1. Design dataport 90000, G/L Account Export Fixed from the Object
Designer.
2. Compile and save the dataport with the ID 90001, and the name G/L
Account Export Variable.
3. Open the Properties window for the dataport, set the following
properties:
o FileFormat: Variable
o FieldStartDelimiter: <None>
o FieldEndDelimiter: <None>
o FieldSeparator: ;
The semicolon is used as the field separator because the dataport fields include
both space characters and commas. Another solution is to use the delimiters. In
that case, the field separator can be a comma. Set these properties appropriately,
depending on the target application for the exported file or which formats that the
application supports when it is importing text files. Leave the RecordSeparator
and the DataItemSeparator properties as they are. This means the records are
separated by new lines and data items are separated by two new lines. Setting the
FileFormat property to Variable tells C/SIDE to ignore values set in the StartPos
and Width properties of the dataport fields.
4. Close the Properties window, and compile, save and close the
dataport.
5. Run the dataport, save the external file as GLAccountVariable.txt
and view the text file.
The database table that is used in this demonstration is the Vehicle table (created
earlier in the course - Chapter 2: Tables.) Before creating the dataport for import,
create the text file to be used as the import file. The following steps show how to
create a fixed format text file that contains records to be imported to the Vehicle
table.
1. In the Object Designer's Dataport List, click the New button. The
Dataport Designer opens.
2. Open the Properties window for the dataport and set the following
properties:
o Import: Yes (This specifies the dataport to be used only for
import.)
o FileFormat: Fixed (This specifies the external file to have fixed
format.)
Decide how the lines in the import file are broken down into dataport fields; each
line becomes a record in the data item. The lines have a fixed format, and by
carefully looking at the layout of the lines, deduce the field starting positions.
5. While the Vehicle data item is still selected, click View, Dataport
Fields. The Field Designer opens.
6. Type the following in the Field Designer:
Because Model and Serial No. are the fields in the primary key of the table, the
records are displayed in an order determined by these fields, not the in the order
shown in the import file.
Possible Errors
Always test imports carefully before using them for production. It is easy to
make errors when deciding how to divide up the lines in the import file. In some
cases, the rearrangement of records generates a run-time error when the dataport
is run. Consider, for example, if an error is made in setting up a dataport field (of
type Integer in the table) so that it is assigned a width that is one character too
wide. For most of the import file, this makes no difference at all - the resulting
trailing space is ignored. But the line that begins with "112Oven..." can provoke a
run-time error if C/SIDE reads 112O instead of 112. The "O" (uppercase "O")
cannot be inserted into an integer field. In cases such as these, it is fortunate to
provoke a run-time error. In other cases, the error may not be detected by
C/SIDE, for example, if the cut between two text fields is positioned incorrectly.
FIAT,1000,Black,5-Speed,15000,01/31/10
FIAT,2000,Ivory,Automatic,40000,03/31/10
MAZDA,5500,Red,5-Speed,30000,02/10/10
MAZDA,7500,Blue,Automatic,25000,01/15/10
These are the only differences between the fixed format dataport and variable
format dataport.
4. Close the Properties window, and compile, save and close the
dataport.
5. Run table 90000, Vehicle, from the Object Designer.
6. Delete all the records in the table, and close the table.
7. Run the dataport. The request form opens.
8. In the request form Options tab, select VehicleVariable.txt as the file
to import, and then click OK.
9. Run table 90000, Vehicle and view the changes. The table now
contains the imported record.
The result of running this dataport is the same as running the dataport with a
fixed format.
XMLPort Fundamentals
XMLports are used to export and import data to and from external XML
document files. XML documents created by XMLports encapsulate the data in
XML format. This enables exchanging information between different computer
systems in a streamlined manner.
The following shows components of an XMLport and how they are related.
XMLports are created and designed in the XMLport Designer which is accessed
from the Object Designer.
Properties
A property is an attribute of an object, or its component, that characterizes and
specifies behavior of the parent in some ways, such as the type of a node in
XMLport.
The XMLport description contains properties that are related to the XMLport
itself and properties that are related to the other components of the XMLport,
such as nodes and request pages.
Triggers
Certain predefined events that occur to an XMLport cause the system to execute
a user-definable C/AL function. The event and the function together are called a
trigger.
• XMLport triggers
• Node triggers
• Request page triggers
Nodes
Instead of fields, XMLports have nodes. Nodes form the structure of XMLport
and the structure of the XML document or text file which is to be imported from
or exported to. Each node has several important properties, some can be accessed
directly from the XMLport Designer. They are as follows:
Node Remarks
Property
Node Name Used to specify the XML node name of the XML element or
attribute. Node names must be entered in the order in which
they appear in the XML document. Parent elements must
precede their child elements. Indent the node names of child
elements under their parent elements by using one
indentation per level. List attributes under the elements that
they define and indent them to the child level.
Node Type Used to specify whether the name in the Node Name
represents data of type element or attribute. The drop-down
list in the NodeType field contains two options: Element and
Attribute. The default setting is Element.
Source Type Used to specify the data structure that the Node Name
corresponds to. The SourceType field contains a drop-down
list that contains three options: Text, Table, and Field. The
default setting is Text.
Data Source Used together with the Source Type to specify the data
source from the data structure.
The Data Source has the following interactions with the Source Type:
Request Page
The request page is the page that is run before the actual XMLport begins
execution. It is used to collect requests and options from the user of the XMLport
of things such as filtering.
Generally, request pages in XMLports and request pages in reports are similar.
They have similar filter functionality grouped in FastTabs and developers can
add more controls by creating other FastTabs.
Request page in XMLport only has OK and Cancel buttons (compared to Print,
Preview and Cancel buttons in request page in reports) regardless whether the
XMLport is used for importing or exporting text or XML documents. Request
page is displayed only when XMLports are run in the RoleTailored client.
Design XMLports
An XML document contains XML nodes which determine the nature of the
content that they contain.
To create an XMLport to import data from an XML document, specify all the
XML nodes (specify node names) and indicate the type of each, whether it
represents an element or an attribute. Map these nodes to corresponding data
structures (tables, records or fields) in the Microsoft Dynamics NAV database.
When an XMLport object is called to handle an incoming XML document, it
reads the incoming data stream and performs the processing and database actions.
For incoming documents of these types, use C/AL code to perform the necessary
database manipulation to achieve the desired result.
XMLport Properties
The XMLport properties describe the XMLport in general. Several properties,
such as the Direction and FileName properties, can be set and reset dynamically.
For example, developers can create an XMLport where the user can do one of the
following:
The Format property determines the format of the external file. In other words, it
determines the XMLport nodes behavior in a record. The following list shows the
options available for the Format property.
Format Remarks
Property
Value
XML The XMLport handles XML documents.
Fixed Text The XMLport handles text file that has fixed format.
Variable Text The XMLport handles text file that has variable format.
Node Properties
The node properties describe the node of the XMLport which defines the
structure of the XMLport. The following list shows examples of node properties.
To test run an XMLport before integrating it to the rest of the application, create
a test codeunit that calls the XMLport and streams data to or from a file,
depending on whether it is an import or an export process. The following shows a
test codeunit to run an XMLport.
1. In the Object Designer's XMLport List, click the New button. The
XMLport Designer opens.
2. Open the Properties window for the XMLport and set the following
properties:
o Direction: Export (This specifies the XMLport to be used only
for export.)
o Format: Xml (This specifies the external file to be an XML
document.)
5. Go to the next line and type the following to add a source table.
Ensure that it is indented under the Root element.
NOTE: The G/L Account table ID is 15. Instead of typing 15 directly to the Data
Source field, use the LookUp button on the Data Source field and select the G/L
Account table.
6. Go to the next line and type the following on the next few lines to
add several fields from the G/L Account table. Ensure that they are
indented under the GL Account element.
NOTE: Instead of typing the value to the Data Source field, use the LookUp
button to open the Field Lookup window and select the fields from the Field
Lookup window.
7. Open the Properties window for the G/L Account element, and set
the following property:
o SourceTableView: SORTING(No.) WHERE(Account
Type=FILTER(Posting|End-Total))
o CalcFields: Balance at Date,Net Change
NOTE: Instead of typing the value directly to the CalcFields property, click the
Assist-Edit button on the CalcFields property to open the Field List window. Add
the fields in the Field List window and then click OK. Also, instead of typing the
value to the SourceTableView property, use the Assist-Edit button to open the
Table View window and assign the property value.
9. Compile and save the XMLport by clicking File, Save As. The Save
As dialog box opens.
10. Type 90000 in the ID field and G/L Account Export XML in the
Name field, ensure that the Compiled check box is selected, and
then click OK. This compiles and saves the XMLport.
1. In the Object Designer's Codeunit List, click the New button. The
C/AL Editor opens.
2. Click View, C/AL Globals.
3. In the Variables tab, type the following:
Name DataType
TestFile File
TestStream OutStream
6. Compile and save the codeunit by clicking File, Save As. TheSave
As dialog box opens.
7. Type 90004 in the ID field and XML Export - G/L Account in the
Name field, ensure that the Compiled check box is selected, and
then click OK. This compiles and saves the codeunit.
8. Run the codeunit and view the result.
9. Locate the new file "C:\GL Account Export.xml" on the system.
Double-clicking this file opens it in the browser. The following
figure shows the XML file:
<Root>
<Vehicle>
<Model>FIAT</Model>
<SerialNo>1000</SerialNo>
<ListPrice>15,000</ListPrice>
</Vehicle>
<Vehicle>
<Model>FIAT</Model>
<SerialNo>2000</SerialNo>
<ListPrice>40,000</ListPrice>
</Vehicle>
<Vehicle>
<Model>MAZDA</Model>
<SerialNo>5500</SerialNo>
<ListPrice>30,000</ListPrice>
</Vehicle>
<Vehicle>
<Model>MAZDA</Model>
<SerialNo>7500</SerialNo>
<ListPrice>25,000</ListPrice>
</Vehicle>
</Root>
1. In the Object Designer's XMLport list, click the New button. The
XMLport Designer opens.
2. Open the Properties window for the XMLport and set the following
properties:
o Direction: Import (This specifies the XMLport to be used only
for import.)
o Format: Xml (This specifies the external file to be an XML
document.)
5. Go to the next line and type the following to add a source table.
Ensure that it is indented under the Root element.
6. Go to the next line and type the following on the next few lines to
add several fields from the Vehicle table. Ensure that they are
indented under the Vehicle element.
7. Compile and save the XMLport by clicking File, Save As. The Save
As dialog box opens.
8. Type 90001 in the ID field and Vehicle Import XML in the Name
field, ensure that the Compiled check box is selected, and then click
OK. This compiles and saves the XMLport.
9. Close the XMLport.
1. In the Object Designer's Codeunit list, click the New button. The
C/AL Editor opens.
Name DataType
TestFile File
TestStream InStream
TestFile.OPEN('C:\VehicleXML.xml');
TestFile.CREATEINSTREAM(TestStream);
XMLPORT.IMPORT(XMLPORT::"Vehicle Import XML",TestStream);
TestFile.CLOSE;
MESSAGE('Vehicle Import Xml completed!');
6. Compile and save the codeunit by clicking File, Save As. TheSave
As dialog box opens.
7. Type 90005 in the ID field and XML Import - Vehicle in the Name
field, ensure that the Compiled check box is selected, and then click
OK. This compiles and saves the codeunit.
8. Run table 90000, Vehicle, from the Object Designer.
9. Delete all the records in the table, and close the table.
10. Run the codeunit and then run table 90000, Vehicle, from the Object
Designer and view the result. The table now contains the imported
record.
In the new three-tier architecture, the business logic is run on the middle-tier
server and not on the client. This means that in Microsoft Dynamics NAV, files
are created on the Microsoft Dynamics NAV service and not locally on the client
computer as is the case in the Classic client. There are several built-in functions
which are used to transfer files from and to Microsoft Dynamics NAV service to
the RoleTailored client, such as the DOWNLOAD and the UPLOAD functions.
In addition, the ISSERVICETIER function is used to determine whether code is
run in the Classic client or Microsoft Dynamics NAV service.
The following steps show how to run the codeunit 90004, XML Export - G/L
Account, from a page in the RoleTailored client.
Name DataType
ToFile Variant
Caption Type
Run Codeunit Action
Caption Type
Run XMLport Action
4. Close the Properties window, and compile, save and close the
XMLport.
5. Design page 90001, Custom Page, from the Object Designer.
6. Open the Action Designer for the page and type the following on the
empty line:
Caption Type
Run XMLport Action
Variable Text
Scenario
Simon has already created a Course table to record course information and Card
and List pages to interface the Course table. Now, Simon must create XMLports
to export all the course details from the Course List page, in the following
format:
Challenge Yourself!
Step by Step
Create an XMLport for export to variable format text file.
1. In the Object Designer's XMLport List, click the New button. The
XMLport Designer opens.
2. Open the Properties window for the XMLport and set the following
properties:
o Direction: Export (This specifies the XMLport to be used only
for export.)
o Format: Variable Text (This specifies the external file to be an
XML document.)
o FieldSeparator: ,
o FieldDelimiter: <None>
5. Go to the next line and type the following to add a source table.
Ensure that it is indented under the Root element.
6. Go to the next line and type the following on the next few lines to
add several fields from the G/L Account table. Ensure that they are
indented under the GL Account element.
7. Compile and save the XMLport by clicking File, Save As. The Save
As dialog box opens.
8. Type 90010 in the ID field and Course Export Variable in the Name
field, ensure that the Compiled check box is selected, and then click
OK. This compiles and saves the XMLport.
9. Close the XMLport.
4. Close the Properties window, and compile, save and close the
XMLport.
Type SubType
ActionContainer ActionItems
4. Type the following on the next lines and indent them under the
ActionContainer.
Caption Type
Run XMLport Action
Variable
Run XMLport Action
XML
Summary
Dataports and XMLports are objects that are used for importing data from and
exporting data to external files.
Use dataports to export data from Microsoft Dynamics NAV or import data into
Microsoft Dynamics NAV instead of XMLports in the following scenarios:
2. What cannot be done with a data item in a dataport that can be done with a
data item in a report?
3. In an import dataport, what must the dataport fields created in the Dataport
Field Designer match?
5. When must the StartPos and Width properties of a dataport field be used?
6. True or False. Every dataport field must be bound to a real field in the data
item.
9. When exporting, which dataport file type creates the smallest possible file?
10. When importing, which dataport properties can be set so that the user cannot
view the options tab?
13. Which field is used to specify whether the name in the NodeName field
represents data of the type element or attribute?
14. Which field specifies the data structure that the node name corresponds to in
the Microsoft Dynamics NAV database?
16. True or False. An XMLport cannot modify existing data in the database.
1.
2.
3.