Sitecore Customize Media Provider

There are scenarios when you have to customize/override Sitecore default media provider. Like in my case we enabled Akamai (CDN) for media items and  requirement was to generate the media items url using Akamai domain rather than site domain.  There are other scenarios also where you may have to customize media provider.

So to customize media provider

  • Create a new public class and inherit it from resource.Media.MediaProvider class and Sitecore.Events.Hooks.IHook Interface, like
public class CustomMediaProvider : MediaProvider, Sitecore.Events.Hooks.IHook
  • Create initialize method
public void Initialize()
        {
            MediaManager.Provider = this;
        }

  • Create constructor of class and create mediacreator object
public CustomMediaProvider ()
        {
            OverrideMediaCreator();
        }

        protected void OverrideMediaCreator()
        {
            Sitecore.Resources.Media.MediaCreator mediaCreator = GetMediaCreator();
            if (mediaCreator == null)
            {
                return;
            }

            Creator = mediaCreator;
        }

        protected virtual Sitecore.Resources.Media.MediaCreator GetMediaCreator()
        {
            return Factory.CreateObject("mediaLibrary/mediaCreator", false) as MediaCreator;
}
  • Override the GetMediaUrl and customize the code as per your need
public override string GetMediaUrl(MediaItem item)
        {
            return GetCDNURL(base.GetMediaUrl(item));
        }

        public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
        {
            return GetCDNUrl(base.GetMediaUrl(item, options));
        }

  • Do a entry in custom patch file
<sitecore>
<hooks>
      <hook  type="Assembly.Namespace.CustomMediaProvider, Assembly Details">
      </hook>
    </hooks>
</sitecore>
  •  Complete code is as follows
public class CustomMediaProvider : MediaProvider,IHook
    {
        public void Initialize()
        {
            MediaManager.Provider = this;
        }

        public override string GetMediaUrl(MediaItem item)
        {
            return GetCDNUrl(base.GetMediaUrl(item));
        }

        public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
        {
            return GetCDNUrl(base.GetMediaUrl(item, options));
        }

        string GetCDNUrl (string url)
        {
            ….
        }

        public CustomMediaProvider ()
        {
            OverrideMediaCreator();
        }

        protected void OverrideMediaCreator()
        {
            Sitecore.Resources.Media.MediaCreator mediaCreator = GetMediaCreator();
            if (mediaCreator == null)
            {
                return;
            }

            Creator = mediaCreator;
        }

        protected virtual Sitecore.Resources.Media.MediaCreator GetMediaCreator()
        {
            return Factory.CreateObject("mediaLibrary/mediaCreator", false) as MediaCreator;
        }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s