You can export ViewModel results to files from MDrivenServer by configuring a periodic server-side action with a savefile action column; this is for developers who need scheduled XML, transformed text, or binary-file exports.
Export files with a periodic server-side action
A periodic server-side action is a ViewModel that MDrivenServer evaluates repeatedly. When MDrivenServer finds an action column named savefile, it writes the ViewModel result to a file.
To create an export:
- Create the ViewModel that selects the data to export and exposes the values needed for the file name and destination.
- Add an action column named
savefile. The name is case-sensitive. - Add columns named
pathandfilename. These names are case-insensitive. - Make the
pathandfilenamecolumns evaluate to the destination directory and file name for each export. - Configure the ViewModel as a periodic server-side action in MDrivenServer. For job configuration and maintenance, see HowTos:Configure and Maintain MDriven Server.
- Run the job and verify that the MDrivenServer process can write to the configured path.
When savefile, path, and filename are present, MDrivenServer serializes the complete ViewModel result as XML using ViewModelXMLUtils.GetDataAsXml and saves it to path/filename.
Required columns
| Column name | Name matching | Purpose |
|---|---|---|
savefile
|
Case-sensitive | Identifies an action that tells the periodic action supervisor to write an export file. |
path
|
Case-insensitive | Evaluates to the destination directory. |
filename
|
Case-insensitive | Evaluates to the name of the file to create. |
Both path and filename are evaluated expressions. This lets one export run create destination-specific or date-specific files when those values are available in the ViewModel.
For example, an export ViewModel can expose data for an article and use expressions that resolve to a directory and a file name such as articles.xml. With no XSLT or filedata column, the saved content is the XML representation of the full ViewModel result.
Control action order and updates
Put any action that records a successful export after the file-writing action. For example, an UpdateExportTime action can update an object's export timestamp after savefile has written the file.
The execution is wrapped in a memory transaction. Therefore, a later action that updates the export time is not committed unless the file write succeeds and the execution completes successfully. This prevents an export timestamp from indicating success when the export file was not written.
| Order | Example action | Result |
|---|---|---|
| 1 | savefile
|
MDrivenServer generates and writes the export file. |
| 2 | UpdateExportTime
|
The model records that the export completed. Place this action last. |
Transform XML with XSLT
Add a ViewModel column named XSLT when the exported file must have a format other than the default ViewModel XML. If this column is present, MDrivenServer treats its value as a valid XSLT transformation, transforms the ViewModel XML, and saves the transformation result to path/filename.
For example, the following XSLT produces text containing an article number, a comma, and the sales article name. Adapt the XPath expressions to the XML structure produced by your ViewModel.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:value-of select="/*/ArticleNumber"/>,<xsl:value-of select="/*/SalesArticleName"/>
</xsl:template>
</xsl:stylesheet>
If the transformed ViewModel XML has an ArticleNumber of NO101087 and a SalesArticleName of Cosmica Tygg 90 ST, the output is:
NO101087,Cosmica Tygg 90 ST
Use XSLT when the export consumer expects a text, CSV-like, or other XML-derived format. The XSLT column content must be valid XSLT.
Export raw file data
Add a ViewModel column named filedata to write file content instead of ViewModel XML.
filedata column value
|
Saved output |
|---|---|
| Image, Blob, or another byte-array type | The raw byte data is written to the file. |
| String | The string content is written to the file. |
For example, if filedata evaluates to a Blob that contains a PDF, MDrivenServer writes the PDF bytes to the path and filename evaluated by the export ViewModel. If it evaluates to a string containing report text, MDrivenServer writes that text instead.
Use filedata for content that is already file data. Use the default export when you need the complete ViewModel XML, or use XSLT when you need to transform that XML first.
Choose the export mode
| Need | ViewModel columns | Result |
|---|---|---|
| Save the complete ViewModel result as XML | savefile, path, filename
|
MDrivenServer serializes the ViewModel result as XML. |
| Save XML in another format | Required columns plus XSLT
|
MDrivenServer applies the XSLT and saves the transformed result. |
| Save bytes or supplied text | Required columns plus filedata
|
MDrivenServer saves the byte-array data or string content. |
