One of Sitecore’s most useful features is the plug-ability of functionality via the configuration factory. Its very easy to add or update custom implementations where necessary.
A typical programming model used throughout this is the Provider model – Membership, Roles, Item, Proxy… the list is endless. Unfortunately one provider thats not exposed in the config factory is the MediaProvider.
No problem, thanks to some help from support they suggested a way to get around this, you can tap into the initialise pipeline. Here is the patch file to enable this:
1 2 3 4 5 6 7 8 9 |
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <pipelines> <initialize> <processor patch:after="processor[@type='Sitecore.Pipelines.Loader.EnsureAnonymousUsers, Sitecore.Kernel']" type="###.Domain.Cms.Specialization.Pipelines.Loader.InitializeMediaProvider, ###.Domain.Cms" /> </initialize> </pipelines> </sitecore> </configuration> |
This pipeline runs as the application intializes. Next you need the implementation of the pipeline processor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using Sitecore.Diagnostics; using Sitecore.Pipelines; using Sitecore.Resources.Media; namespace ###.Domain.Cms.Specialization.Pipelines.Loader { /// <summary> /// Sets the Provider for Sitecore's MediaManager to a custom version. /// </summary> public class InitializeMediaProvider { public void Process(PipelineArgs args) { Assert.IsNotNull(args, "args"); MediaManager.Provider = new ###.Domain.Cms.Specialization.Resources.Media.MediaProvider(); } } } |
And finally the custom implementation:
1 2 3 4 5 6 7 8 9 10 |
namespace ###.Domain.Cms.Specialization.Resources.Media { public class MediaProvider : Sitecore.Resources.Media.MediaProvider { public override string GetMediaUrl(MediaItem item, MediaUrlOptions options) { ... } } } |
Our customization allowed us to push certain media extensions to known file types.
Very interesting, thank you for sharing!
I really like your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you? Plz reply as I’m looking to create my own blog and would like to know where u got this from. many thanks
It’s a wordpress theme 🙂
I’ve been searching far and wide for a solution like this. THank you very much!
Any thoughts on how to do this in Sitecore 8, since that MediaManager._Provider_ property was deprecated?
Have you tried experimenting with the updated config e.g. :
mediaProvider type="Sitecore.Resources.Media.MediaProvider, Sitecore.Kernel"