Server

HOWTO: Export Data Using Document Template and Automation

Updated: November 15, 2024

Document Templates are pattern files used to produce any type of text-based document with dynamic content. Used in combination with automation chains, document templates can be used to generate files such as CSV files, MS Word or OpenOffice file, PDF files, XML files, HTML, etc.

In a few steps, we will show how to generate a CSV file that lists all the documents of your Nuxeo Platform repository and some metadata.

Step 1: Creating the Document Template

You can choose between two languages to create a document template: MVEL and FreeMarker. Here we will use FreeMarker. If you are not familiar with this language, you can check out their documentation.

In this example we want to create a CSV file that displays a list of documents with some metadata. CSV is a quite simple format that is often used to present tabular data.

column_name1, column_name2, column_name3
data_A1, data_B1, data_C1
data_A2, data_B2, data_C2
..., ..., ...

This will produce a table like this:

column_name1 column_name2 column_name3
data_A1 data_B1 data_C1
data_A2 data_B2 data_C2

To create the template:

  1. Go to Templates -> Document Templates.
  2. Click on the New button to create a new document template.
  3. Give your new template the ID "CSVExport" and click on the Next button. Now we are ready to fill in the template.
  4. The first line of the template will represent the column of the generated CSV file. Since those column names never change, type them as we want them to appear in the final document:

    Title, Description, Creator, Creation Date
    
    
  5. The template will enable to handle a list of documents. For each of these documents, we will need to retrieve some of its metadata and display them.

    <#list This as doc>
    "${doc["dc:title"]}","${doc["dc:description"]}","${doc["dc:creator"]}","${doc["dc:created"]}"
    </#list>
    
    

    The template now looks like that:

    Title, Description, Creator, Creation Date
    <#list This as doc>
    "${doc["dc:title"]}","${doc["dc:description"]}","${doc["dc:creator"]}","${doc["dc:created"]}"
    </#list>
    
  6. Click on Save to save your template.

Notes

  • Double quotes between elements are optional (CSV format).
  • If you are going to use the template in order to create an HTML file, you can put HTML tags in the template.
  • The only thing that differs from Nuxeo usual syntax is FreeMarker's <list> element.
  • Within a document template, whether you use FreeMarker or MVEL, you can use the same variables, functions and XPath expressions that are used in automation chains. As document templates and automation chains frequently work together, each context variable set in the automation chain can be used in the template.

Other FreeMarker Examples

  • A common use case is retrieving values from a vocabulary list. Vocabularies are composed of IDs and labels, and we want to get the Label of vocabulary elements. To do that we will use this function:

    "${Fn.getVocabularyLabel("mymetadata",doc["myschemaprefix:mymetadata"])}"
    
    
  • The element dc:subjects is a list, so it has to be listed as one using the script.

    <#list doc["dc:subjects"] as subject>
    ${subject}
    </#list>
    
    

Step 2: Using the Document Template

We are going to create an automation chain to query the Nuxeo repository and render the query result as a template-based CSV file.

The chain should look like this:

- Context.FetchDocument
- Document.Query:
    query: "SELECT * FROM Document"
    language: NXQL
- Render.DocumentFeed:
    template: "template:CSVExport"
    filename: mygeneratedfile.csv
    mimetype: text/csv
    type: ftl

Notes

  • In this example, we query all the documents in the repository. You can define the query to filter the content repository and display only the documents that meet the query conditions. See the page NXQL for more information about Nuxeo SQL-like query language.
  • To download a file in an HTML format instead of CSV, use the .html extension in the filename and choose text/xml as the mimetype.

Step 3: Creating the Button to Generate the CSV File

The only step left is to create a button (User Action -> New Action Feature) that will trigger this operation chain. Don't forget to check the Download attribute checkbox so that the result of the automation chain is downloaded on the client machine.