> For the complete documentation index, see [llms.txt](https://developer.vario-software.de/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.vario-software.de/documentation/scripting/files-and-export.md).

# 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**:

1. **Create the target.** Either a DMS document with an output stream, or a string collector.
2. **Create a writer** on that target with `ctx.services.ioFactory`: a plain writer for free text formats or an XML writer.
3. **Write** the content line by line or element by element.
4. **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

| Target               | Created with                                                                                                                                                                                         | Result                                                                                                                           |
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| **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](/documentation/scripting/output.md)), 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.

| Method                                               | Description                                      |
| ---------------------------------------------------- | ------------------------------------------------ |
| `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.

| Method                                                     | Description                                                                                   |
| ---------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
| `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](/documentation/scripting/http-and-secrets.md).

***

## Errors

| Message                                                                                  | Cause                                                  |
| ---------------------------------------------------------------------------------------- | ------------------------------------------------------ |
| "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 |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developer.vario-software.de/documentation/scripting/files-and-export.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
