Exercise - Create an XMLport to export XML data
Scenario
You're a developer working for CRONUS International Ltd. You want to create an XMLport to export all customer information from the Customer List page in XML format.
Tasks
Create a new extension.
Create an XMLport.
Create a page extension.
Export data by using a stream.
Steps
Start Visual Studio Code.
Select View > Extensions (Ctrl+Shift+X).
Enter AL Language in the Search Extensions in the Marketplace search box.
Select the green Install button.
Create a new AL extension project. Select View > Command Palette... (Ctrl+Shift+P).
Enter AL: Go! in the search box and select the command from the list.
Accept the suggested path (or enter another path).
Select the latest target platform.
Select Microsoft cloud sandbox as the development endpoint.
Download the application symbols. Select View > Command Palette... (Ctrl+Shift+P).
Enter AL: Download symbols in the search box and select the command from the list.
If requested, provide your organizational credentials (Microsoft 365 account/Microsoft Entra ID account).
Open the app.json file and change the name to Customer XML. Change the publisher to Cronus International Ltd.
Remove the HelloWorld.al file.
Create a new file named: CustomerXml.XmlPort.al.
Enter txmlport and then press the Tab key.
Change the ID to 50103 and the name to Customer XML.
Set the Caption to Export Customers(s) to XML
Set the XMLport property Direction to Export and the Format property to XML.
Change the text element of the snippet to Customers.
Under Customers, create a TableElement. Change the name to Customer and set the SourceTableName to Customer.
Add a FieldAttribute for the Number and Language Code fields.
Create a FieldElement for the Name and the Phone No. fields.
Create a TextElement for the Address field. Set the XMLName property to Address.
Create a FieldElement for the Address, Post Code, and City fields within the Address text element.
Remove the RequestPage section.
xmlport 50103 "Customer XML" { Direction = Export; Format = Xml; Caption = 'Export Customers(s) to XML'; schema { textelement(Customers) { tableelement(Customer; Customer) { fieldattribute(Number; Customer."No.") { } fieldattribute(Language; Customer."Language Code") { } fieldelement(Name; Customer.Name) { } fieldelement(Phone; Customer."Phone No.") { } textelement(Address) { XmlName = 'Address'; fieldelement(StreetAndNr; Customer.Address) { } fieldelement(Zipcode; Customer."Post Code") { } fieldelement(City; Customer.City) { } } } } } }
Create a new file, named CustomerList.PageExt.al.
Enter tpageext and then press the Tab key.
Change the ID to 50101 and the name to CustomerList.
Change the object to extend the Customer List page.
In the Actions section, add a new action with the name ExportToXml. Set the caption to Export to XML. This action should be added as the last action in the &Customer group.
In the OnAction trigger, create a variable called CustomerXml of type XMLport Customer XML. Use the SetDestination function to set an OutStream. Call the Export function on the CustomerXml variable.
pageextension 50101 CustomerList extends "Customer List" { layout { // Add changes to page layout here } actions { // Add changes to page actions here addafter("&Customer") { action(ExportToXml) { Caption = 'Export to XML'; ToolTip = 'Export to XML'; ApplicationArea = All; Image = XMLFile; trigger OnAction() var TempBlob: Codeunit "Temp Blob"; CustomerXml: XmlPort "Customer XML"; OutStr: OutStream; InStr: InStream; FileName: Text; begin TempBlob.CreateOutStream(OutStr); CustomerXml.SetDestination(OutStr); CustomerXml.Export(); TempBlob.CreateInStream(InStr); FileName := 'Customers.xml'; File.DownloadFromStream(InStr, 'Download', '', '', FileName); end; } } } var myInt: Integer; }
Open the launch.json file and change the startupObjectId to 22, which is the Customer List.
Publish your extension to the sandbox. Select View > Command Palette... (Ctrl+Shift+P).
Enter AL: Publish in the search box (or press the F5 shortcut key) and select the command from the list.
Select the Export to XML action in the menu, if you want to test the XMLport functionality.