Thanks to the data source excerpt file concept, it is very easy to create templates with complex structures such as nested repeating blocks. A data source excerpt file is used to fill the drop-down lists of the template editor with proper merge fields, relations and dummy data for a template preview. A data source excerpt file is not the actual data that is used to merge the template. It helps to design the templates and to insert the proper field names.

In this article, the following .NET classes are used as the data source structure for the ReportingCloud merge process:

public class Report
{
public Report()
{
this.Sales = new List<Sale>();
}
public string Name { get; set; }
public List<Sale> Sales { get; set; }
}
public class Sale
{
public Sale()
{
this.Items = new List<Item>();
}
public int Id { get; set; }
public List<Item> Items { get; set; }
}
public class Item
{
public int Qty { get; set; }
public float Price { get; set; }
public Product Product { get; set; }
}
public class Product
{
public string Name { get; set; }
}
view raw rc.cs hosted with ❤ by GitHub

Report is the master template which reflects the complete template. In other words, the complete template is merged with the number of rows in this master template. The following code shows the instantiated .NET object that is used:

Report report = new Report();
report.Name = "Monthly Sales Report";
report.Sales.Add(new Sale()
{
Id = 1,
Items = new List<Item>()
{
new Item()
{
Price = 256.2F,
Product = new Product()
{
Name = "ReportingCloud"
},
Qty = 5
},
new Item()
{
Price = 424.2F,
Product = new Product()
{
Name = "TX Text Control"
},
Qty = 3
}
}
});
view raw rc.cs hosted with ❤ by GitHub

This object has 1 Sale object which consists of 2 different Item objects. In the template, a list of all Sale items should be visualized as a merge block. Inside this outer merge block, all Item objects should get listed as a nested merge block. The following screenshot shows this structure in the template:

Merging nested repeating blocks in ReportingCloud

But how to insert these blocks in the ReportingCloud template designer?

First, we need to create a serialized JSON string from the object. You can create this string manually or simply serialize this object using the JavaScriptSerializer class:

var json = new JavaScriptSerializer().Serialize(report);
view raw tx.cs hosted with ❤ by GitHub

The results will look similar to this:

{
"Name":"Monthly Sales Report",
"Sales":[
{
"Id":1,
"Items":[
{
"Qty":5,
"Price":256.2,
"Product":{
"Name":"ReportingCloud"
}
},
{
"Qty":3,
"Price":424.2,
"Product":{
"Name":"TX Text Control"
}
}
]
}
]
}
view raw test.json hosted with ❤ by GitHub

In the ReportingCloud Portal, open My Datasource Excerpts and create a new excerpt file by adding a name and pasting the JSON string to the excerpt input. As ReportingCloud expects an array of objects as the data source, enclose the complete string with square brackets.

Merging nested repeating blocks in ReportingCloud

Now, when designing the template in the ReportingCloud template designer, the merge blocks are listed in the available Insert Merge Block drop-down button. In the ReportingCloud Portal, create a new template in the My Templates area. Before editing the template, select the created datasource excerpt file from the available sources and confirm with Edit Template.

Merging nested repeating blocks in ReportingCloud

  1. In the opened template designer, click on sales from the Insert Merge Block drop-down to insert the outer merge block sales.

  2. Set the input position into the block row and add a table row using the right-click context menu.

    Merging nested repeating blocks in ReportingCloud

  3. Set the input position into the newly created table row and choose items from the Insert Merge Block drop-down to insert the nested block items.

  4. In the opened dialog, select the 2 columns price and qty and confirm with OK.

    Merging nested repeating blocks in ReportingCloud

A second nested block has been inserted into the outer block:

Merging nested repeating blocks in ReportingCloud

When previewing this template, the expected results of the nested block are rendered successfully:

Merging nested repeating blocks in ReportingCloud

Test this on your own and create a free trial account today.