How to migrate MediaFile custom fields?

The method MediaFileK13.FromReader() doesn't allow for Media File custom fields.

I am migrating my K13 media files to Content Items and need to migrate a custom field I have called PhotoSource.

Do I need to overwrite FromReader after each git pull or is there some other way I can get these custom fields?

public static MediaFileK13 FromReader(IDataReader reader, SemanticVersion version) => new(
        reader.Unbox<int>("FileID"), reader.Unbox<string>("FileName"), reader.Unbox<string>("FileTitle"), reader.Unbox<string>("FileDescription"), reader.Unbox<string>("FileExtension"), reader.Unbox<string>("FileMimeType"), reader.Unbox<string>("FilePath"), reader.Unbox<long>("FileSize"), reader.Unbox<int?>("FileImageWidth"), reader.Unbox<int?>("FileImageHeight"), reader.Unbox<Guid>("FileGUID"), reader.Unbox<int>("FileLibraryID"), reader.Unbox<int>("FileSiteID"), reader.Unbox<int?>("FileCreatedByUserID"), reader.Unbox<DateTime>("FileCreatedWhen"), reader.Unbox<int?>("FileModifiedByUserID"), reader.Unbox<DateTime>("FileModifiedWhen"), reader.Unbox<string?>("FileCustomData")
    );
Tags:
Migration / upgrade

Answers

Accepted answer

Hi RoryAherneSOS,

You don't need to touch FromReader at all - and I'd strongly avoid it. That method is internal plumbing for the tool's media-file asset reader, so overwriting it means re-applying your patch after every git pull, and it's not the intended extension point anyway. It only reads the fixed CMS_File system columns because media files are migrated into Content hub as content item assets.

See the "Media libraries" section: https://github.com/Kentico/xperience-by-kentico-kentico-migration-tool/blob/master/Migration.Tool.CLI/README.md#media-libraries

Your PhotoSource field lives on the Media file system class, and that's covered by a different command. The --custom-modules migration explicitly includes the customizable system classes and their custom fields, and "Media libraries > Media file" is one of them:

So, the supported approach is:

1. Include --custom-modules in your run so the Media file class + PhotoSource come across, e.g.

Migration.Tool.CLI.exe migrate --sites --custom-modules --users --media-libraries

2. Because the files land as Legacy.Mediafile content items, use the Migration.Tool.Extensions project to map the custom field onto the target - via a class mapping (IClassMapping) - instead of editing tool source. Kentico even ships a sample for exactly this scenario: AddCustomContactFieldMappingSample, which shows how to migrate custom fields added to system tables (e.g. OM.Contact). The Media file class is the same pattern.

Rough shape (real API - MultiClassMapping + BuildField):

var m = new MultiClassMapping("Legacy.Mediafile", _ => { });

m.BuildField("PhotoSource")
 .SetFrom("Media.File", "PhotoSource", true);

// Register it
serviceCollection.AddSingleton<IClassMapping>(m);

Then call your extension method from UseCustomizations in Migration.Tool.Extensions/ServiceCollectionExtensions.cs and rebuild the tool before running.

Two things worth knowing:

  • A class mapping replaces the default migration for that source class, so if you map only some fields, the rest of that class won't migrate automatically — map every field you need.

  • Verify the exact code names against your own instance (Legacy.Mediafile as the target class, and Media.File / PhotoSource as your source class/field names). The mechanism is documented, but the names should be confirmed in your DB rather than trusted blindly.

Docs and the shipped sample:

One heads-up: migrating media to media libraries (MigrateMediaToMediaLibrary) is now deprecated - content item assets is the default and recommended target, so plan for PhotoSource to land on the asset content type.

Hope that helps!

To response this discussion, you have to login first.