One of the biggest changes in EPiServer 7 to 7.5 is without doubt the new system for handling assets and media. The old VPP folders are gone, replaced by a BLOB storage which gives us strongly typed access to all our files. Furthermore every asset now implements IContent and as such can be handled just like any PageData or BlockData object.

This means we can add properties to media assets and select which type of files should correspond to which type of MediaData class.

The whole process is quite simple. Just create a class which inherits EPiServer.Core.MediaData, decorate it with a ContentType attribute and optionally a MediaDescriptorAttribute to specify which type of files should correspond to this particular class.

[ContentType(GUID = "BB0099B6-5C65-4D37-9812-ED0D3E12BC2F")]
[MediaDescriptor(ExtensionString = "pdf")]
public class PdfMedia : MediaData
{
   public virtual int VersionNumber { get; set; }
}

In this example I’ve created a ContentType that will be used for all files uploaded that have the pdf-extension.

Note how you can add properties to your content just like you would to any Page- or BlockData and they will be automatically available in edit mode.

There’s however one problem with this approach, there’s no simple way to convert an already created MediaData ContentType to another ContentType.

I am working with a client that has thousands (if not tens of thousands) of pdf-files uploaded. They have recently discovered the need to add properties to pdf-files, handling things such as version number, revision date, author etc. Unfortunately they had no configured ContentType for pdf-files at the time of uploading the files to EPiServer which means that they are of the generic MediaData type and they are unable to add properties to them.

You can see exactly what I mean if you install the Alloy MVC template and upload a pdf file to any content folder. Then add a MediaData class as the one in the example above. You’ll note that your uploaded PDF file will still be of the “Generic File” type and not of your new and shiny PdfMedia type.

generic file type

This is unfortunate but not very surprising. Converting between content types can be dangerous and could in the worst case result in loss of data.

However, sometimes you just have to live dangerously. I’ve created this simple plugin to handle converting media types:

convert media types

It basically does the same thing as the EPiServer tool to convert between pagetypes, but this only applies to Media ContentTypes.

If I type in 163 and run the tool my Generic File is now a PdfMedia.

pdf media type

This is enormously handy as any properties stored on the MediaDataType will automatically be converted if it exists on the target type and even saved between conversions. This means that if you have a MediaData type Generic File that has the properties Description and Author and convert a file of this type to PdfMedia the properties will not be lost! Any data entered will still be left in the database and if you were to realize that you’ve made a mistake and wish to convert back to a Generic File you can do so and the properties will be retained.

With that said, this tool operates directly on the database and loss of data is possible if you’re not careful or try something crazy. I would recommend taking a backup of your database before running large operations with this tool. I take no responsibility for any data loss or other crazy effects.

The backend code of the tool uses the EPiServer.DataAccess.ConvertPageTypeDB class that “is not intended to be used directly from your code”, but well…

How does a crazy person walk through the forest?