How To Import Content Into Episerver Automatically Using the In-built Importer And Some Code

Out of the box, Episerver provides content editors a WSYWIG admin screen that allows them to select a bunch of content within the website and export it to a file.  It also has the mirror service to import that content.  If you want to learn more about this feature I suggest you read How To Export and Import Content Within Episerver.

This process is cool but if you want to set-up a continuous integration and deployment process, having to manually copy a file and then going into the editor to import it goes against the ethos of automation.  Luckily, Episerver does have an API that you can use to automate this content importing step.  If you want to learn how to be a Ci guru, then read on.

Creating A .episerverdata 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) to see the contents of the file. Within the .episerverdata file, you will see a number of XML files that define the content, 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:

  
                var dataImporter = ServiceLocator.Current.GetInstance<IDataImporter>();
                var startNode = new ContentReference(5);
                var filePath = "C:/App_Data";

                var fileStream = new FileStream(filePath, FileMode.Open);
                var options = ImportOptions.DefaultOptions;
                options.KeepIdentity = true;
                options.ValidateDestination = true;
                options.EnsureContentNameUniqueness = false;
                options.IsTest = false;

                dataImporter.Import(fileStream, startNode, options);
  

I'll quickly go over the interesting parts. To import the file, we read it from disk, bung it in a file stream and then with a few other properties, call the IDataImporter -> Import() method. As part of this process, you will need to pass in a DefaultOptions object.

I would recommend:

  • 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 App_data Folder Paths Location In Code.

Importing Takeaway

As you can see, if you want to automatically import content into an EPiserver website with code, it's fairly straight-forward and pain-free. If you want to build up your continuous integration process and have content automatically inserted into your build process then I suggest you start with this approach first as it's the quickest.

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.

 

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top