Exercise - Create an XMLport to export XML data

Completed

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

  1. Start Visual Studio Code.

  2. Select View > Extensions (Ctrl+Shift+X).

  3. Enter AL Language in the Search Extensions in the Marketplace search box.

  4. Select the green Install button.

  5. Create a new AL extension project. Select View > Command Palette... (Ctrl+Shift+P).

  6. Enter AL: Go! in the search box and select the command from the list.

  7. Accept the suggested path (or enter another path).

  8. Select the latest target platform.

  9. Select Microsoft cloud sandbox as the development endpoint.

  10. Download the application symbols. Select View > Command Palette... (Ctrl+Shift+P).

  11. Enter AL: Download symbols in the search box and select the command from the list.

  12. If requested, provide your organizational credentials (Microsoft 365 account/Microsoft Entra ID account).

  13. Open the app.json file and change the name to Customer XML. Change the publisher to Cronus International Ltd.

  14. Remove the HelloWorld.al file.

  15. Create a new file named: CustomerXml.XmlPort.al.

  16. Enter txmlport and then press the Tab key.

  17. Change the ID to 50103 and the name to Customer XML.

  18. Set the Caption to Export Customers(s) to XML

  19. Set the XMLport property Direction to Export and the Format property to XML.

  20. Change the text element of the snippet to Customers.

  21. Under Customers, create a TableElement. Change the name to Customer and set the SourceTableName to Customer.

  22. Add a FieldAttribute for the Number and Language Code fields.

  23. Create a FieldElement for the Name and the Phone No. fields.

  24. Create a TextElement for the Address field. Set the XMLName property to Address.

  25. Create a FieldElement for the AddressPost Code, and City fields within the Address text element.

  26. 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) { }
                     }
                 }
             }
         }
     }
    
  27. Create a new file, named CustomerList.PageExt.al.

  28. Enter tpageext and then press the Tab key.

  29. Change the ID to 50101 and the name to CustomerList.

  30. Change the object to extend the Customer List page.

  31. 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.

  32. 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;
     }
    
  33. Open the launch.json file and change the startupObjectId to 22, which is the Customer List.

  34. Publish your extension to the sandbox. Select View > Command Palette... (Ctrl+Shift+P).

  35. Enter AL: Publish in the search box (or press the F5 shortcut key) and select the command from the list.

  36. Select the Export to XML action in the menu, if you want to test the XMLport functionality.