Files & Export
Write CSV, XML and free text files from scripts into the DMS or into a string for an API push.
VARIO Cloud scripts can produce files in any text format: CSV for a price portal, XML for a logistics partner, fixed-width records for a bank or any other format a partner expects. The file lands as a document in the DMS or is collected as a string and sent to an API.
For standard formats, check the built-in automated data exports first. A script is the right tool when the format is custom or the file has to go straight to an interface.
How an Export Works
Every export combines a writer with a target:
Create the target. Either a DMS document with an output stream, or a string collector.
Create a writer on that target with
ctx.services.ioFactory: a plain writer for free text formats or an XML writer.Write the content line by line or element by element.
Close the writer. Closing flushes the content and closes the target. Only then is the DMS file stored or the string complete.
A writer and its target can be used exactly once. Writing after closing fails with "Der Writer wurde bereits geschlossen und kann nicht weiter verwendet werden."
Targets
DMS document
shelfDocumentService.create(fileName, documentTypeKey) creates an empty document of a DMS document type; shelfDocumentService.createDmsOutputStream(documentId, fileName) opens a stream into it
When the writer is closed, the content is stored as a file of that document. Each closed stream adds a new file to the document.
String collector
ioFactory.createStringCollector()
getContent() returns everything written so far as a UTF-8 string, for example as the body of an HTTP request
A DMS document can then be linked to a record with shelfDocumentService.createAttribution(), attached to an e-mail with attachDocument() on an output request (see Output), or picked up by a person in the DMS.
Plain Writer — Free Text Formats
The plain writer writes text without any format rules. Use it for CSV, TXT, fixed-width records, JSON or any other text format you assemble yourself.
ioFactory.createPlainWriter(target)
Writer with Windows line breaks (CR_LF)
ioFactory.createPlainWriter(target, linebreakType)
Writer with the line break CR_LF, LF or CR
write(text)
Appends text
writeLine(text)
Appends text followed by a line break
close()
Finishes the file and closes the target
The plain writer always writes UTF-8. Separators, quoting and escaping are up to the script: if a CSV value can contain the separator or a quote, quote it yourself.
XML Writer
The XML writer produces well-formed XML. Element names, attributes and text are written through method calls, and text and attribute values are escaped automatically.
ioFactory.createXmlWriter(target, encoding, prettyPrint)
Writer with the given encoding, e.g. UTF-8; prettyPrint indents nested elements with tabs
writeProlog(encoding, version)
Writes the XML declaration, e.g. with UTF-8 and 1.0; not written automatically
openElement(name)
Opens an element
writeAttribute(name, value)
Adds an attribute to the element that was just opened
writeCharacters(text)
Writes text content
writeCData(text)
Writes a CDATA block
writeComment(text)
Writes a comment
writeEmptyElement(name)
Writes a self-closing element
closeElement()
Closes the current element
closeAllOpenElements()
Closes all elements that are still open
close()
Finishes the document and closes the target
Attributes must follow directly after openElement() or writeEmptyElement(), before any text or child element.
Exporting Many Records
Each script run starts with a fresh context, so a writer cannot stay open across the rows of a batch run. For an export over a selection:
Collect the lines per row in
ctx.importSession, which keeps its values across all rows of a run.Write the file once in
setAfterBatch. It only runs if at least one row was processed without error.IMPORTANT Keep Parallele Ausführung erlauben off
Inside a single workflow run or a single row, create, write and close the file in one go.
Pushing a File to an API
Write into a string collector instead of a DMS document, then send getContent() as the body of a POST or PUT request with httpClientService. The request body is limited to 10 MiB, and the target must be on the HTTP whitelist, see HTTP & Secrets.
Errors
"Der Writer wurde bereits geschlossen und kann nicht weiter verwendet werden."
The script writes after close()
"Der Ziel-Stream wurde bereits geschlossen und kann nicht weiter verwendet werden."
A second writer uses a target that is already closed
"Der Ziel-Stream ist noch offen. Der Schreibvorgang muss zunächst abgeschlossen werden."
The DMS file is requested before the writer was closed
Last updated
Was this helpful?