How to export data programmatically in Episerver.

Diederik Mathijs
2 min readMar 11, 2021

--

Photo by Andy Li on Unsplash

Episerver provides us with a great GUI tool to export data from within the application. The idea is that you select your root and can then configure the export to include specific block-types, configure the recursion depth, …

This is great but for your use-case you might also want to do this programmatically. For this purpose, EPiServer exposes the IDataExporter. The IDataExporter has a method called ‘Export’ with 3 input parameters:

The stream parameter is a stream to which the exporter will write. This can be e.g. a FileStream. The sourceRoots defines the ‘root’ objects from where the exporter will export, these roots also have their own recursion level which allows us to configure the depth of the export. The last parameter contains the options that apply to the export and are the options that are also configurable in the GUI.

Knowing all of this, it becomes pretty easy to actually do our export. There is a small catch that you have to pay attention to to not end up with a corrupt export file. Let’s go through an example.

The example is straightforward. We start by resolving a IDataExporter ServiceAccessor for later use. In the export method, we create a new list of ‘ExportSource’ objects which contains only one item. This item tells the exporter to start the export at the root of the content tree. Note that we’re passing int.MaxValue as the recursion level. This means that there’s endless recursion which in turn means that the whole tree will be exported.

After that, we define some options. All options are annotated with an XML-summary so I won’t go over every single option. The only important thing I want to note concerning the options is that I’ve set the ‘AutoCloseStream’ option to false. Why do you ask? You’re disposing of the stream after exporting anyway? I want to bring it to the developers’ attention that this is an important flag.

Because, unlike a stream adapter, the exporter doesn’t just dispose the inner stream once it’s done. It will write more information to the stream! This means that you have to ensure that the exporter is disposed of before flushing/disposing of your inner stream! Otherwise, you’ll end up with a corrupted stream

On line 20 we do exactly that, we define a using statement with a new file-stream and a new exporter. We pass the stream/exportSource/options to the export method. After that, FIRST the exporter is disposed of. This will trigger it to write additional bytes to our stream and AFTER that, the stream will be disposed of, flushing the additional data and you’ll end up with a valid .episerverdata file.

Et voila, a working data export tool for Episerver.

Hopefully you enjoyed this article, feel free to contact me with further questions.

--

--

Diederik Mathijs
Diederik Mathijs

Written by Diederik Mathijs

Senior .NET/React developer writing about technical topics surrounding Javascript.

No responses yet