Out-of-the-box, Episerver provides content editors with a content import/export feature. This feature is available from the Episerver admin UI. The export process allows content editors to select any content that has been added within the Episerver page tree and export it to a file. It also has a mirror service to read an export file and automatically import content into the CMS. If you want to learn more about this feature I suggest you read How To Export and Import Content Within Episerver.

This process is useful to move content between different environments. Moving content between environments is a common task, especially when work goes from the development environment into the client review environment. Would it not be cool to be able to add this feature into your Ci/Cd pipeline so content could automatically be read in? This would save a lot of time as editors and developers would no longer need to manually undertake this process. Luckily, Episerver does have an API that you can use to automate this content importing step. If you want to learn how to be an import/export automation guru, then read on.

Creating An Episerver Export File

First, we need to create an Episerver export file that will contain all the pages, images, and assets that we want to include as part of our import process. Creating this export file is a manual job and will require you to log into Episerver, select the pages you want to include in your export file (usually the homepage and everything below it). The result of this export process will be a .episerverdata file that gets generated in your browser. Very shortly, we'll be adding this file into your App_data folder within your website and checking it into source control.

For those of you who are interested, you can unzip this file on your PC (I use 7-Zip as my tool of choice) to see the contents of the file. Within the .episerverdata file, you will see a number of XML files that define the content and a blob folder that contains all the images and assets that are linked to.

Now We Have An Export File, What's Next?

Next, we'll need to write some code to read in the file and import it into Episerver. This can be done using the snippet:

To import the file, it is retrieved from the folder within App_data. To work with the file in code, it is converted into a file stream. with the file as a stream, you can use the Import() method found on IDataImporter. As part of this process, you will need to pass in a DefaultOptions object. The setting I recommend using with this call are:

  • IsText = false
  • KeepIdentity as true. When KeepIdentify is enabled, the import will keep the same GUID that it had when it was exported. This will ensure none of your start-up code should break, as the ID's won't change between environments.
  • EnsureContentNameUniqueness = false. From my experiments, if you select you might end up with duplicate content if you run the script twice by accident :P
  • If you're wondering how to get your App_Data folder in code, then have a look at, How To Get The Episerver AppData Folder Paths Location In Code.

Importing Takeaway

As you can see, if you want to automatically import content into an Episerver website via code, it's fairly straightforward. Adding this code in an initialization module will allow you to import content automatically within a Ci/Cd pipeline if you want to. If you find that your team breaks the importing process on a frequent basis, or you have more advanced needs like requiring logic to determine what gets created, then the next step would be to have a look at How To Install A New Episerver Website Via Code. Happy Coding 🤘