Kentico CMS Quick Tip - Enhance your Media Library File Metadata
Introduction
One of the things that I love about Kentico is that the platform and the people who work on it are very smart. Sometimes eerily smart. In tonight’s blog post I am going to describe a real world scenario where I thought I would have to customize the system to pull off a feature that I needed for my own purposes at Mcbeev.com.
As it turns out, I was wrong, no customization was required. The system was already built in a way that made my requirement dead simple to solve. Kentico never stops impressing me even after using it for 5+ years.
Keep reading to see how this quick tip will allow you to add and manage metadata to Kentico Media Library files without dropping into any code.
The Scenario
As you may or may not know, I host a podcast called Kentico Rocks alongside Bryan Soltis. We talk monthly (…well close to monthly…) on Kentico best practices, upcoming developer community events, and in general about the Kentico platform. It’s actually something I enjoy quite a bit.
When it comes to podcasts one of the best ways to distribute them is to setup the podcast in iTunes so that people can subscribe to cast. To do so you need to host each mp3 file that contain the actual episode recording. Once you host the files, you can create a specific RSS feed that has the metadata about each episode and where to download or stream the file from and publish that feed. Apple will then spider the RSS feed, once it is approved by them ... of course… and that is how iTunes shows the updated episodes. The result looks like this.
As you can see the iTunes application makes a nice display of the RSS feed, and takes the pieces of information such as Name, Time (duration), the Release Date, Description, etc. etc. and displays them to the user.
If you notice the screen shot above shows how I am not great at remembering to update all fields, especially the Time field. The reason for this is that I power the podcast files and RSS feed right from my Kentico blog here at Mcbeev.com. I use the standard Kentico Media Library feature to upload the mp3 files as we record them. I then quickly used the Media Files Data Source web part to be the data source for the content of a RSS Feed web part. With a few minor configurations to the web parts I quickly was able to have an automatically populating RSS feed that works for iTunes.
The solutions looks like:
I have re-purpose the standard Title and Description fields that a Kentico Media Library file natively comes with for the iTunes Name and Description fields. However, the Time field is not a standard field in the Media file class. And because it is not a standard field there is no direct place in the user interface to edit it. Or so I thought…
When I originally started this process I looked at the class of Media File in the Kentico Media Library module. I noticed that there is a FileCustomData field. Since I needed one piece of custom information I went with just using the field, and manually updated the database table when I needed to for the first 8 episodes. Yes I know, it is a no good, very back, evil hack. I probably would not have passed any kind of BizStream code review if the guys back at the shop found out.
Fast Forward to Now
Tonight I wanted to fix the solution and come up with a way to have an editable field in the Media File user interface that I could use for my purposes. I fired up Visual Studio and navigated to the /CMSModules/MediaLibrary/Controls/MediaLibraryMediaFileEdit.ascx source file. In a quick scan of the code I started to see a mention of formMediaFileCustomFields. Which sounded pretty darn interesting to me.
And sure enough there was some built in code to the Kentico platform that turned a tab visible if the MediaFile class had been customized.
HasCustomFields = formMediaFileCustomFields.FormInformation.GetFormElements(true, false, true).Any();
if (HasCustomFields)
{
...
tabCustomFields.Visible = true;
plcMediaFileCustomFields.Visible = true;
}
I was so close! The only thing I did wrong back when I set this up was to use the FileCustomData field, if I would have used the system to add an additional field into the Media File class that would have totally opened up the built in form to edit custom fields for Media Files.
PRO TIP: Don’t go crazy customizing system level classes in Kentico. It could endanger your upgrade path and ease of upgrade process.
So if you add any additional fields to the Media File table like the screen shot below:
Then the user interface automatically shows a new tab for managing a Media File’s custom fields.
The Final Quick Tip
The quick tip simply boils down to if you want to add extra metadata to a Kentico Media Library file, go right ahead! The platform totally supports it, and will show a custom tab that allows you edit and save any custom fields very easily. The steps are simple navigate to the Modules application. Edit the Media Library module. Then navigate to the Classes vertical tab and edit the Media File class to include any extra fields or metadata that you want to extend the files to include and click Save.
As soon as you save that return to the Media Library, select a file and the Custom Fields tab should appear.