Use XMLports in AL code

Completed

Though you can integrate XMLports into the user interface, actual situations in which users would be directly exporting and importing data are infrequent. AL programming language provides several functions to help you control the behavior of XMLports programmatically.

In addition to importing data from and exporting data to external files, XMLports provide streaming capabilities to help you access data directly, regardless of the source or destination type. Instead of having to know the technical capabilities of the specific source or destination, programmers can access them consistently and with little effort.

Occasionally, the direction of the XMLport and source or destination can't be predetermined by you, can't be left to the user's choice, and must be controlled programmatically during run time. In these situations, you can use AL functions to dynamically determine whether the XMLport will import or export data and which stream will be used as the source or destination.

The AL functions that support streaming through XMLports are:

  • SetSource - Specifies the InStream variable to be used as the source for the data import.

  • SetDestination - Specifies the OutStream variable to be used as the destination for the data export.

  • Import - Imports the data from the designated variable by using the SetSource function.

  • Export - Exports the data into the designated variable by using the SetDestination function.

The following code sample shows the XMLport Export Contact being run in code. The SetDestination function is used to set the OutStream variable. The Temp Blob codeunit is used to convert an OutStream into an InStream that can be used to prompt the user to download the export as an XML file. You can also use this InStream to send the XMLport data to an external web service.

pageextension 50100 ContactList extends "Contact List"
{
    layout
    {
        // Add changes to page layout here
    }

    actions
    {
        // Add changes to page actions here
        addlast("F&unctions")
        {
            action(ExportContact)
            {
                ApplicationArea = All;
                Caption = 'Export Contact(s)';
                ToolTip = 'Export Contact(s)';
                Image = ExportContact;

                trigger OnAction()
                var
                    FileManagement: Codeunit "File Management";
                    TempBlob: Codeunit "Temp Blob";
                    XmlExportContact: XmlPort "Export Contact";
                    InStr: InStream;
                    OutStr: OutStream;
                    FileName: Text;
                begin
                    begin
                        FileName := 'ExportContacts.xml';

                        TempBlob.CreateOutStream(OutStr);
                        XmlExportContact.SetDestination(OutStr);
                        XmlExportContact.Export();

                        TempBlob.CreateInStream(InStr);
                        File.DownloadFromStream(InStr, 'Download XML Export', '',
                                                FileManagement.GetToFilterText('', FileName),
                                                FileName);
                    end;
                end;
            }
        }
    }

    var
        myInt: Integer;
}