How to set custom fields on media items?

How can I add custom fields to a media item?  For example, in our current project we need to be able to classify documents that are added to the media library as either Type A, Type B or Type C.  Later we will use that for faceted search.  I know I could create a content item that links to the media item, but I would like to simply add that Type field directly to the media item itself.

  • You could create a new template with the fields you require on and then inherit from the required media template. If you want the fields on more create a template with the fields and then create a templates for each media item template.

    Example:

    Template: MediaTemplateNewContent
    Fields:  Field 1
             Field 2
             Field 3
    
    Template: New Jpeg Template
    Inherits: MediaTemplateNewContent
              Jpeg
    
    etc...

    Once you have done that you will need to create an include file that modifies the mediaTypes section of the config to let Sitecore use your new templates when uploading files:

    <mediaTypes>
    	<mediaType name="JPEG image" extensions="jpg, jpeg, jpe, jfif">
    		<mimeType>image/jpeg</mimeType>
    		<forceDownload>false</forceDownload>
    		
    		<!-- PATCH THESE 2 VALUES -->
    		<sharedTemplate>system/media/unversioned/jpeg</sharedTemplate>
    		<versionedTemplate>system/media/versioned/jpeg</versionedTemplate>
    		<!-- PATCH THESE 2 VALUES -->
    		
    		<mediaValidator type="Sitecore.Resources.Media.ImageValidator" />
    		<thumbnails>
    			<generator type="Sitecore.Resources.Media.ImageThumbnailGenerator, Sitecore.Kernel">
    				<extension>png</extension>
    			</generator>
    			<width>150</width>
    			<height>150</height>
    			<backgroundColor>#FFFFFF</backgroundColor>
    		</thumbnails>
    		<prototypes>
    			<media type="Sitecore.Resources.Media.JpegMedia, Sitecore.Kernel" />
    		</prototypes>
    	</mediaType>
    </mediaTypes>

    g

    This is just an example of the mediaType config, you would want to patch the sharedTemplate and versionedTemplate fields.

    This would avoid changing the Sitecore Templates directly which is not good practice. Also in future versions, any changes Sitecore make to the system templates would automatically be inherited in your templates.

  • In reply to Richard Seal:

    Thanks so much for the quick reply. That seems like so much custom work to go through to get attributes/fields added to media items. I am thinking that this has to be something that has been tackled before. I am thinking that adding custom attributes/fields to media items is not something that is out of the ordinary. Is there some other way of accomplishing that that is more "out of the box" Sitecore functionality?
  • In reply to Corey Burnett:

    Sorry, I didn't fully read your question. If you just want to classify media items, why not use the tagging field (Semantics) and create a tag for each of the types. That would be simple also to create a facet on.

    If you want new fields on media items, then your only real options would be to edit the standard Sitecore template, or implement something like the outline above.
  • All templates which Sitecore uses when you upload a file in Media Library are placed under "/sitecore/templates/System/Media". If with "Type A, B, C..." you mean "Pdfs, Images, Docs..." you can use TemplateIDs in your search to search only in Images or Pdfs or whatever.

    If you really need custom field to classify documents, the easiest way is to create your custom template and set it as a base template for all media templates where you need it.

       

    Because it is not a good practice to modify Sitecore system templates, the other option is to extend these templates using them as base template for your custom template and write a custom processor in <uiUpload> pipeline.

    Helpful resources:

    http://stackoverflow.com/questions/10503857/change-template-of-uploaded-file-in-media-library-in-sitecore

    https://sdn.sitecore.net/Articles/Media/Prevent%20Files%20from%20Uploading/Pipeline%20upload/Second%20example.aspx

  • In reply to Rosen Petrov:

    I've always felt that Sitecore should automatically create a customer owned template for media items and set the upload configuration to point at that. By the time you realize you want to add a field you might be stuck with a bunch of images on the old template and be in a situation where you need to change the template for a bunch of images to keep things consistent.

    Is my understanding correct?
  • In reply to Peter Garner:

    Yes, you are right. The used templates are system and because you will need to be consistent after the change of template with custom one, you will need to change the templates of all old media items.