In this tutorial, you will learn how you can do content imports using Episerver CMS. The migration of content (also known as "lift and shift") is the process of copying content from an existing platform into the new website.
Content migration can take a long time on a new project. When companies switch CMS platforms, change technology stacks or even upgrade between versions of the same CMS, in a lot of situations, you will need to get the data from the old system into the new system.
When thinking about content migration, a lot of people will autoamtically think its a code only process. My first tip surrounding content migration is to clearly think if the time, cost, and loss of development effort will be worth it 🤔
Manually migrating content in large sites can take weeks or months. Hiring some content editors to copy and paste content can often be a lot more cost-effective than getting a developer to design and implement a migration script. There are definitely a lot of situations where an automatic script/process makes sense. Maybe you need to run the scripts more than once, maybe you don't have access to content editors. Regardless of the reason, if you need to do this in Episerver, where do you start? Read on to learn how I recommend you bulid your first script 🔥🔥🔥
In Episerver, it makes a lot of sense to add your content migration code within a scheduled task, why?
A schedule task will allow you to run the script mulitple times easily
Scheduled tasks are only available in the admin, so noddy editors wont be able to access it
Scheduled tasks run outside the page lifecycle, meaning they can run for hours without timing out!
There's not a lot of other options 😊
NOTE: If you are new to Episerver scheduled tasks and you want to learn more about them, I recommend you read this tutorial.
For this example, the first thing we need is some JSON. I will use this JSON schema today:
Let us assume we want to import the title and keyword values and then export the data into an Episerver page of type
The Scheduled Task
First, create a new class that implements from
JobBase. This class need to implement the
[ScheduledPlugIn] attribute. For this task, I will define a few properties that will allow me to display import progress back to the Episerver admin UI. These details will include how many files we have imported, how many have failed, and how long it took. As in any Episerver scheduled job the real meat happens in the
Execute() method. Here we use the
FileHelper class to read in all the JSON files. The
FileHelper looks like this:
After we get all the files, I use Json.Net to populate a custom C# object I've created called
This object will be automatically populated via JSON.Net. Afterwards it is passed into a method that will import the object as an Episerver page.
NOTE: If you have never come across JSON.Net before, I recommend reading this. The code to do all this can be seen here:
The object is populated with all the data from the JSON file. I then set any custom properties (like the parent node where the page should be created). and use the content repository to create a new page within Episerver based on the objects data:
With the code within
ContentPageRepository, looking like this:
These are all the steps that you need to follow in order to import JSON files as CMS content within Episerver. We've created a custom scheduled task that reads JSON files in from an Import directly locate in our Webroot and then converts the data using Json.Net into a page of type
ContentPage. All the above code can be downloaded in a fully working website from my GitHub account here. #ILoveEpiserver. Happy Coding 🤘