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:
kontent-ai / management-sdk-net
Kontent.ai Management .NET SDK
Kontent.ai Management .NET SDK
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);
}
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.
Top comments (0)