loading...
TrueLime

Migrate Kentico EMS to Kentico Kontent - Part 2: Assets

jeroenfurst profile image Jeroen Fürst ・2 min read

Migrate Kentico EMS to Kentico Kontent (3 Part Series)

1) Migrate Kentico EMS to Kentico Kontent - Part 1: Taxonomy 2) Migrate Kentico EMS to Kentico Kontent - Part 2: Assets 3) Migrate Kentico EMS to Kentico Kontent - Part 3: Projects

Introduction

In the last post I shared code examples on how to migrate your Kentico EMS Categories into Kentico Kontent Taxonomies. This post builds on top of those examples and illustrate how you can migrate Kentico EMS Media Library files to Assets in Kentico Kontent. A difference compared to importing Taxonomies is that this post uses the Kentico Cloud Content Management .NET SDK:

GitHub logo Kentico / kontent-management-sdk-net

Kentico Kontent Management .NET SDK

Kentico Kontent Management .NET SDK

Build status Stack Overflow

Package Version Downloads Documentation
Management SDK NuGet NuGet 📖
Content Item Edit-URL Builder NuGet NuGet 📖

Summary

The Kentico Kontent Management .NET SDK is a client library used for managing content in Kentico Kontent. It provides read/write access to your Kentico Kontent projects.

You can use the SDK in the form of a NuGet package to migrate existing content into your Kentico Kontent project or update content in your content items. You can import content items, their language variants, and assets.

The Management SDK does not provide any content filtering options and is not optimized for content delivery. If you need to deliver larger amounts of content we recommend using the Delivery SDK instead.

You can head over to our Developer Hub for the complete Management API Reference.

Prerequisites

To manage content in a Kentico Kontent project via the Management API, you first need to activate the API…

Work breakdown

Before we dig into the code, it is good to have a understanding of what kind of asset data Kentico Kontent requires. Adding assets consists out of the following two steps:

  • Upload the binary file as a file reference
  • Create the asset and pass in the file reference

So let's start with the first step by grabbing the media file, uploading it to Kentico Kontent and retrieving the file reference.

Download the media file

In Kentico EMS media files are typically referenced using a permanent link (e.g. to prevent broken links when renaming or moving files). This can make it complex to find the actual file that we want to migrate. You could use the API to retrieve it from the database, write some code to locate the file in the media library folder or download the file from the live website. In my case I went with the last option and downloaded the media file using the following code:

using (WebClient webClient = new WebClient())
{
   byte[] data = webClient.DownloadData(imageUrl);
}

In order to create a FileReference we need two more things: the name of the file and the content type.

Grab the filename

The filename should be relatively easy. Either take it from the Media File using the Kentico API or by executing a SQL query or use code like the following:

Uri uri;
if (!Uri.TryCreate(imageUrl, UriKind.Absolute, out uri))
   uri = new Uri(domainName, imageUrl);

var fileName = Path.GetFileName(uri.LocalPath);

Get the content type

As for the content type we could use the FileExtensionContentTypeProvider taken from the Microsoft.AspNetCore.StaticFiles NuGet package:

new FileExtensionContentTypeProvider().TryGetContentType(source, out contentType);

This should be enough info to get the FileReference via the Content Management .NET SDK:

FileReference fileReference = await client.UploadFileAsync(
   new FileContentSource(data, fileName, contentType));

Upsert the asset

The final step of the exercise is to create the asset, pass in the FileReference and a Title (e.g. the Filename) and upserting it using a external ID (remember the previous post?). By passing in the external ID we will be able to insert and/or update (upsert) our Asset without having to worry if it exists or not 👍

AssetUpsertModel asset = new AssetUpsertModel
{
   FileReference = fileReference,
   Title = fileName
};

AssetModel assetResponse = await client.UpsertAssetByExternalIdAsync(assetExternalId, asset);

And that's all to it! In this post you have seen a different approach to migrating Kentico EMS data to Kentico Kontent using the Kentico Cloud Content Management .NET SDK.

Migrate Kentico EMS to Kentico Kontent (3 Part Series)

1) Migrate Kentico EMS to Kentico Kontent - Part 1: Taxonomy 2) Migrate Kentico EMS to Kentico Kontent - Part 2: Assets 3) Migrate Kentico EMS to Kentico Kontent - Part 3: Projects

Posted on by:

jeroenfurst profile

Jeroen Fürst

@jeroenfurst

Hi there! I am Jeroen, Kentico MVP and Architect at TrueLime (Web Agency in The Netherlands).

TrueLime

TrueLime is expert in developing web applications, websites, customer portals and social intranets.

Discussion

markdown guide