DEV Community

Jeroen Fürst for TrueLime

Posted on

Migrate Kentico EMS to Kentico Kontent - Part 2: Assets

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 kontent-ai / management-sdk-net

Kontent.ai Management .NET SDK

Kontent.ai Management .NET SDK

Build & Test codecov Stack Overflow Discord

Package Version Downloads Compatibility Documentation
Management SDK NuGet NuGet net8.0 📖
Content Item Edit-URL Builder NuGet NuGet net8.0 📖

Summary

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

You can use the SDK in the form of a NuGet package to migrate existing content into your Kontent.ai project or update your content model.

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.

💡 If you want to see all .NET related resources including REST API reference with .NET code samples for every endpoint, check out the "Develop .NET apps" overview page.

Prerequisites

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

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);
}
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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));
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)