DEV Community

Jeroen Fürst for TrueLime

Posted on • Updated on

Migrate Kentico EMS to Kentico Kontent - Part 1: Taxonomy

Introduction

I have recently been working on a project with the goal to migrate all the content from Kentico EMS to Kentico Kontent. In this post I will share code examples on how to migrate the Kentico EMS categories to Kentico Kontent taxonomies.

Note: at the moment of writing this post, the Kentico Kontent Content Management SDK does not target the v2 API. Therefor the code will be based on executing a HttpWebRequest and posting the json data.

Enable the Content Management API

We will need to the enable the Content Management API in Kentico Kontent and setup the Project ID and API Key:

var apikey = "YOUR_API_KEY";
var projectid = "YOUR_PROJECT_ID";
Enter fullscreen mode Exit fullscreen mode

Create the HttpWebRequest

Using a HttpWebRequest we will pass in the necessary authorization headers:

var url = $"https://manage.kenticocloud.com/v2/projects/{projectid}/taxonomies";

var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("Authorization", $"Bearer {apikey}");
httpWebRequest.Method = "POST";
Enter fullscreen mode Exit fullscreen mode

Get the JSON

I am a big fan of SQL and it's is quite easy to return the CMS Categories from Kentico EMS in a JSON format that matches the data definition that the Taxonomy endpoint requires. By adding FOR JSON AUTO to the end of your query will automagically transform the output to JSON. 👍

var query = @"SELECT [CategoryDisplayName] AS [name], 
                     'tax-term-' + cast([CategoryID] as varchar(10)) AS [external_id],
                     '[]' AS [terms]
              FROM 
                     [CMS_Category]
              ORDER BY
                     [CategoryDisplayName]
              FOR JSON AUTO";
Enter fullscreen mode Exit fullscreen mode

Note: the provided example code only supports 1 level of categories. You will have to write some additional code to support the hierarchy of categories by passing in nested terms.

What I really like about the Kentico Kontent Content Management API is that you can use external ID's to make a reference to your original data. This allows you to apply updates to your content without having to do the whole migration process over again. 👍

Execute the SQL query

Execute the SQL query and get the JSON result back. A simple example can be found below:

var taxonomy = new StringBuilder();

using (var conn = new SqlConnection("YOUR_CONNECTION_STRING"))
{
   conn.Open();
   SqlCommand command = new SqlCommand(query, conn);

   using (SqlDataReader reader = command.ExecuteReader())
   {
      if (reader.HasRows)
      {
         while (reader.Read())
         {
            taxonomy.Append(Convert.ToString(reader[0]));
         }
      }
   }
}
Enter fullscreen mode Exit fullscreen mode

Finalize the JSON

The JSON returned from SQL contains all the categories (a.k.a. terms) that we will pass to Kentico Kontent as a new Taxonomy. To finalize the JSON data we will have to add the Taxonomy Group that will contain the Taxonomy terms:

var finaljson = @"{'name': 'Name of Taxonomy Group',
                   'external_id': 'tax-term-001',
                   'terms': " + json + @"}";
Enter fullscreen mode Exit fullscreen mode

Post the JSON data via the HttpWebRequest

The last step consists out of posting the JSON data using the HttpWebRequest that we created earlier on in this post:

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{  
   streamWriter.Write(finaljson );
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
   var result = streamReader.ReadToEnd();
}
Enter fullscreen mode Exit fullscreen mode

And that is all! Hope this helps 😄

Top comments (2)

Collapse
 
seangwright profile image
Sean G. Wright

I completely forgot SQL Server can output JSON. Thanks for the reminder.

I could see developers that are testing out Kontent want to seed data from an existing project, so this is a nice example of how we can do that.

👍

Collapse
 
johnram98468527 profile image
JohnRamsey

Good post, I was able to glean useful information here. I would also like to share one useful piece of advice with you, which is to recommend a company that will help you make quality EU Taxonomy Reporting celsia.io/ . Since they have developed a system up to date with the latest updates of EU legislation and industry practice, this allows them to greatly simplify this assessment process.