1

I need to insert text for a media item when getting a media type of PDF by changing the GetMediaUrl method. This is for an RTE field. I went to override the MediaProvider, but found it was obsolete and replaced by the MediaManager. I created a class that inherits from the BaseMediaManager. Now I am wondering how to add that class to the pipeline. I see the one for MediaProvider, but can't figure out where to add one for MediaManager.

I found this for Media Provider:

<mediaProvider type="Sitecore.Resources.Media.MediaProvider, Sitecore.Kernel"/>

Thanks

5
  • Which Sitecore version is used? Commented Jan 25, 2018 at 17:21
  • Currently using 8.2 update 4.
    – RReilley
    Commented Jan 25, 2018 at 17:28
  • Looks like this is the issue. So would this be a duplicate question? I will flag for delete if so. sitecore.stackexchange.com/questions/7348/…
    – RReilley
    Commented Jan 25, 2018 at 18:28
  • 2
    As you found out, mediaProvider is not a pipeline, it is a single type setting. MediaProvider has also (incorrectly?) been marked as [Obsolete] but no alternative has been provided to use, and Sitecore itself is using it as the default provider, so you'll have to ignore the warning, maybe disable it...
    – jammykam
    Commented Jan 25, 2018 at 18:45
  • @jammykam yep. Not sure after all I will need to change it or not yet. I am still looking through the methods, but I need to change the text of the link. The GetMediaUrl just returns the link. I may post another question if I can't find it.
    – RReilley
    Commented Jan 25, 2018 at 18:56

1 Answer 1

8

You need register the new Media Manager using Sitecore Dependency Injection (see 'Registration in code' chapter). It can be done via Service Configurator as described in the article.

Creating Service Configurator

Implement Sitecore.DependencyInjection.IServicesConfigurator interface from Kernel assembly. Please note that you need to reference Microsoft Dependency Injection 1.0.0 NuGet package:

public class ServiceConfigurator : IServicesConfigurator
{
    public void Configure(IServiceCollection serviceCollection)
    {
        serviceCollection.AddSingleton<BaseMediaManager>(s =>
            new MyMediaManager(
                new DefaultMediaManager(
                    s.GetService<BaseFactory>(),
                    s.GetService<MediaProvider>()
                )));
    }
}

Implementing Media Manager

You may inherit the DefaultMediaManager class as you mentioned but in case of numerous changes required (probably by different teams) composition might be a better choise):

public class MyMediaManager : BaseMediaManager
{
    private readonly BaseMediaManager _mediaManager;

    public MyMediaManager(BaseMediaManager mediaManager)
    {
        _mediaManager = mediaManager;
    }

    public override Media GetMedia(MediaItem item)
    {
        // add you custom logic here
        return _mediaManager.GetMedia(item);
    }
    ...
}

Registering Service Configurator

Last step, add the following registration to your configuration files:

<configuration>
  <sitecore>
    <services>
      <configurator type="<younamespace>.DependencyInjection.ServiceConfigurator, <yourassembly>"/>
    </services>
  </sitecore>
</configuration>
2
  • Thanks. I was able to override it. The method that I needed to add the text to is not in the media manager though I found out. I started another question for that. If I needed to change the URL though you answer works.
    – RReilley
    Commented Jan 26, 2018 at 14:08
  • Also, do we need to patch in the mediaprovider with new MediaManager ?
    – Abhay Dhar
    Commented Dec 26, 2019 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.