Create an XMLport by using snippets in Visual Studio Code

Completed

To create an XMLport in Visual Studio Code, you can use the txmlport snippet, which will generate a template structure that you can modify for your needs.

An XMLport contains two sections called schema and requestpage. In the schema section, you can define the structure of your XMLport. You can use textelement, tableelement, fieldelement, or fieldattribute to define the nodes. In the requestpage section, you can define the layout and actions for the page that is displayed to the user when an XMLport runs. You can use the same layout definition as a card page.

Work with XML format

The following code sample provides information on how you can work with XML format by using Visual Studio Code and the AL programming language.

xmlport 50100 MyXmlport
{
    Caption = 'Export Contacts to XML';
    Direction = Export;
    Format = Xml;
    UseRequestPage = false;
    DefaultFieldsValidation = true;

    schema
    {
        textelement(Contacts)
        {
            XmlName = 'Contacts';

            tableelement(Contact; Contact)
            {
                RequestFilterFields = "No.";
                XmlName = 'Contact';

                fieldattribute(No; Contact."No.")
                {
                }
                fieldattribute(ExternalID; Contact."External ID")
                {
                }
                fieldelement(Name; Contact.Name)
                {
                }
                fieldelement("E-Mail"; Contact."E-Mail")
                {
                }
                fieldelement(HomePage; Contact."Home Page")
                {
                }
                textelement(Company)
                {
                    XmlName = 'Company';

                    fieldattribute(CompanyNo; Contact."Company No.")
                    {
                    }
                    fieldelement(CompanyName; Contact."Company Name")
                    {
                    }
                }
            }
        }
    }

    requestpage
    {
        layout
        {
            area(content)
            {
            }
        }

        actions
        {
            area(processing)
            {  
            }
        }
    }
}

Work with fixed text format

When you configure the XMLport to use the FixedText format, make sure that you specify the Width property for each node in the XMLport. An example of how to complete this action is shown in the following code sample.

xmlport 50100 MyXmlport
{
    Caption = 'Export Contacts to Fixed Text';
    Direction = Export;
    Format = FixedText;
    TextEncoding = UTF8;
    UseRequestPage = false;

    schema
    {
        textelement(Contacts)
        {
            tableelement(Contact; Contact)
            {
                RequestFilterFields = "No.";

                fieldelement(No; Contact."No.")
                {
                    Width = 10;
                }
                fieldelement(ExternalID; Contact."External ID")
                {
                    Width = 10;
                }
                fieldelement(Name; Contact.Name)
                {
                    Width = 20;
                }
                fieldelement("E-Mail"; Contact."E-Mail")
                {
                    Width = 50;
                }
                fieldelement(HomePage; Contact."Home Page")
                {
                    Width = 50;
                }
                fieldelement(CompanyNo; Contact."Company No.")
                {
                    Width = 10;
                }
                fieldelement(CompanyName; Contact."Company Name")
                {
                    Width = 20;
                }
            }
        }
    }
}

Work with variable text format

When configuring the XMLport to use the VariableText format, you can use the FieldSeparator to specify the string that is used to separate fields. The default value is a comma (,).

  • <None> - No field separator

  • <NewLine>: Combination of CR and LF characters

  • <CR/LF>: First a CR followed by LF

  • <CR>: ASCII value 13

  • <LF>: ASCII value 10

  • <Tab>: Use a Tab to separate the fields

  • Other strings: The literal string entered

xmlport 50100 MyXmlport
{
    Caption = 'Export Contacts to XML';
    Direction = Export;
    Format = VariableText;
    FieldSeparator = ',';
    TextEncoding = UTF8;
    ...
}

The RecordSeparator property is used to indicate what should happen at the end of a record. It has the same values as the FieldSeparator property. The default value is <NewLine>.

xmlport 50100 MyXmlport
{
    Caption = 'Export Contacts to XML';
    Direction = Export;
    Format = VariableText;
    FieldSeparator = ',';
    RecordSeparator = '<NewLine>';
    TextEncoding = UTF8;
    ...
}

The FieldDelimiter property is used to set the string that is used to delimit a field.

xmlport 50100 MyXmlport
{
    Caption = 'Export Contacts to XML';
    Direction = Export;
    Format = VariableText;
    FieldSeparator = ',';
    FieldDelimiter = '"';
    TextEncoding = UTF8;
    ...
}

If you get data from different tables in Business Central, and you want to use them in an XMLport, you can use the TableSeparator field to get different sets of nodes.

A typical example of different data sources in an XMLport is when you want to import or export a text file with a header. You can use the Integer table to create a header, and then use the TableSeparator property to separate the two table elements. The OnBeforePassVariable function is used to get the caption of a field, and you can use that caption as the header in the text document.

xmlport 5050 "Export Contact"
{
    Caption = 'Export Contact';
    Direction = Export;
    Format = VariableText;
    TableSeparator = '<NewLine>';
    TextEncoding = UTF8;
    UseRequestPage = false;

    schema
    {
        textelement(Root)
        {
            tableelement(Integer; Integer)
            {
                XmlName = 'ContactHeader';
                SourceTableView = sorting(Number) where(Number = const(1));
                textelement(ContNoTitle)
                {
                    trigger OnBeforePassVariable()
                    begin
                        ContNoTitle := Contact.FieldCaption("No.");
                    end;
                }
                textelement(ContNameTitle)
                {
                    trigger OnBeforePassVariable()
                    begin
                        ContNameTitle := Contact.FieldCaption(Name);
                    end;
                }
                textelement("ContE-MailTitle")
                {
                    trigger OnBeforePassVariable()
                    begin
                        "ContE-MailTitle" := Contact.FieldCaption("E-Mail");
                    end;
                }
            }
            tableelement(Contact; Contact)
            {
                RequestFilterFields = "No.";
                XmlName = 'Contact';
                fieldelement(No; Contact."No.")
                {
                }
                fieldelement(Name; Contact.Name)
                {
                }
                fieldelement("E-Mail"; Contact."E-Mail")
                {
                }
            }
        }
    }
}