Add a Word layout to a report

Completed

A Word report layout is a based on a Word document (.docx file type) that acts as a template for viewing and printing reports from Dynamics 365 Business Central. Word report layouts enable you to design report layouts by using Word. Word report layouts are built on custom XML parts in Word. A custom XML part is structured XML that represents the dataset of a Business Central report. The custom XML part is used to map the data into a report at run time. To create a Word layout report, you'll use Microsoft Word.

The system requirements are Microsoft Word 2016 or later. You might need to download and install the required software before you start to develop report layouts in Business Central.

Let’s look at the following report:

al-languageCopy
report 50100 ExampleWORDLayout
{
    Caption = 'ExampleWORDLayout';
    UsageCategory = ReportsAndAnalysis;
    ApplicationArea = All;
    DefaultRenderingLayout = ExampleWORDLayout;

    dataset
    {
        dataitem(Customer; Customer)
        {
            column(CustomerNo; "No.") { }
            column(CustomerName; Name) { }
            column(City; City) { }
            column(BalanceLCY; "Balance (LCY)") { }
        }
    }

    rendering
    {
        layout(ExampleWORDLayout)
        {
            Type = Word;
            LayoutFile = './src/Reports/WORD/ExampleWORDLayout.docx';
            Caption = 'ExampleWORDLayout';
            Summary = 'An example of a WORD Layout.';
        }
    }
}

To enable multiple layouts, you must use the rendering section of a report object. Inside the rendering section, you define one or more layout sections. In each of the layout sections, you specify details about the layout file path and name, you provide a Caption property and a Summary property which will be displayed to the user in the Report Layouts page in Business Central.

If you don't specify a caption, the layout name will be displayed to the user.

The default layout can be specified with the DefaultRenderingLayout property report property. This property can't be set on report extension objects, only on report objects.

Reports using the previous property-based layout specification can be converted to use the rendering section by using a code action. To use this, ensure code that actions are switched on in your AL extension settings and place the cursor on any of the old layout properties to use the action. Layouts of type RDLC, Word, Excel, and Custom can be specified with the new rendering syntax.

To generate the layout, build the extension (Ctrl+Shift+B).The ExampleWORDLayout.docx file will be created in the root of the current project.

To open the generated report layout file, right-click the Example_WORDLayout.docx file and select Open Externally, as shown in the following screenshot.

Screenshot of the Open Word layout menu.

Now, you can edit the layout in Microsoft Word.

In Word, edit the layout by using the XML Mapping Pane on the Developer tab. If you don't see the Developer tab, go to Options > Customize Ribbon, and in the Main tabs section, select the Developer option.

Screenshot of the Customize ribbon pane with developer highlighted.

In Word, to the right, in the Custom XML part lookup, locate the report dataset.

Screenshot of the Word XML mapping pane.

Insert a table in the Word document. The table requires two rows and a column for each field in the dataset that you want to add.

Screenshot of the insert a table in Word feature.

In the XML Mapping pane, right-click the control that corresponds to the report data item that contains the fields that you want repeated and then select Insert Content Control > Repeating. Typically, in a table, the repeater will be on the second row because the first row will act as a placeholder for the column names.

Position the cursor in the second row and then select the whole row.

Screenshot of the select a row feature in Word.

Right-click the Customer table, and in Insert Content Control, select Repeating to add the repeater data item.

Screenshot of the insert a repeater feature in Word.

The second row in the table has now become a repeater.

Screenshot of the result of inserting a repeater.

This row will act as a placeholder for the repeating fields.

Label and data fields of a report are contained in content controls in Word. The following figure shows a content control when it is selected in the Word document.

Screenshot of the content control in Word.

The name of the label or data field name will display in the content control. In the example, the field name is CompanyAddr1.

Right-click the CustomerNo field, and in Insert Content Control, select Plain Text to add the column as a text box.

Screenshot of the insert content control in plain text in Word.

Do not select Rich Text because rich text controls are not fully supported. If you use a rich text control, content might not be displayed correctly.

You can't add a field by manually entering the dataset field name in the content control. You must use the XML Mapping pane to map the fields.

Repeat this step for the other columns in the table and fields in your dataset.

The report would now resemble the following screenshot, if you're creating the steps.

Screenshot of the result of inserting content control in plain text.

In the first row, enter the column names, for example No., Name, City, and Balance.

Screenshot of the type in column labels.

Now, you can save and then close Microsoft Word.

In Visual Studio Code, press the Ctrl+F5 keyboard shortcut to compile and run the report in Dynamics 365 Business Central. In Business Central, use the Tell Me button to search for the report and run it. The generated report will display in preview mode.

Screenshot of the resulting layout in Word.

A report dataset can include a field that contains an image, such as a company logo or a picture of an item. To add an image from the report dataset, insert a picture content control.

Images will align in the upper-left corner of the content control and will resize automatically in proportion to fit the boundary of the content control.

You can only add images that have a format (such as .bmp, .jpeg, and .png file types) that are supported by Word. If you add an image that has a format that is not supported by Word, you'll get an error when you run the report.