Adding a custom field to an Image template
This post is about adding an extra field let’s say “Caption” to an image template in media library. This caption will display with the image on the site.
A very simple straight forward step is to add the field to the standard image template “/sitecore/templates/System/Media/Versioned/Image”. But the problem here is that this image template will get RESET when we upgrade new version of Sitecore.
To avoid this problem, create a new template(for example Media Image) by inheriting the template “/sitecore/templates/System/Media/Versioned/Image”. And use this new template whenever we need to upload an image.
To make use of this new template by “Upload files” in media library, recommended approach is to create a new .config in app_config/include folder and add below contents to the config file.
<?xml version=”1.0″?>
<configuration xmlns:x=”http://www.sitecore.net/xmlconfig/”>
<sitecore>
<mediaLibrary>
<mediaTypes>
<mediaType name=”Windows Bitmap image” extensions=”bmp”>
<mimeType>image/png</mimeType>
<forceDownload>false</forceDownload>
<sharedTemplate>system/media/Versioned/Media Image</sharedTemplate>
<versionedTemplate>system/media/Versioned/Media Image</versionedTemplate>
<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.ImageMedia, Sitecore.Kernel” />
</prototypes>
</mediaType>
<mediaType name=”GIF image” extensions=”gif”>
<mimeType>image/png</mimeType>
<forceDownload>false</forceDownload>
<sharedTemplate>system/media/Versioned/Media Image</sharedTemplate>
<versionedTemplate>system/media/Versioned/Media Image</versionedTemplate>
<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.ImageMedia, Sitecore.Kernel” />
</prototypes>
</mediaType>
<mediaType name=”PNG image” extensions=”png”>
<mimeType>image/png</mimeType>
<forceDownload>false</forceDownload>
<sharedTemplate>system/media/Versioned/Media Image</sharedTemplate>
<versionedTemplate>system/media/Versioned/Media Image</versionedTemplate>
<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.ImageMedia, Sitecore.Kernel” />
</prototypes>
</mediaType>
<mediaType name=”JPEG image” extensions=”jpg, jpeg”>
<mimeType>image/png</mimeType>
<forceDownload>false</forceDownload>
<sharedTemplate>system/media/Versioned/Media Image</sharedTemplate>
<versionedTemplate>system/media/Versioned/Media Image</versionedTemplate>
<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.ImageMedia, Sitecore.Kernel” />
</prototypes>
</mediaType>
</mediaTypes>
</mediaLibrary>
</sitecore>
</configuration>
Now if we upload an image, media library will use our new template Media Image which will give us an extra caption field.
Creating a new config file will not impact pre-existing images to using the old templates.