5

I'm programatically inserting some JPGs into the Media Library with the MediaCreator and then putting URLs to those images in a Rich Text field another item (page). When I call MediaManager.GetMediaUrl(item) I get a URL that begins with /sitecore/shell/-/media and the anonymous user can't see these images.

How do I get a URL to a media item that is visible to the anonymous user?

Here's my code

var mediaCreator = new MediaCreator();
var options = new MediaCreatorOptions
{
      Versioned = false,
      IncludeExtensionInItemName = false,
      Destination = _settings.NewsMediaRootItem.Paths.ContentPath + "/" + fileName,
      OverwriteExisting = true                        
};

using (new SecurityDisabler())
{
    var item = mediaCreator.CreateFromStream(ms, fileName, options);
    var uri = MediaManager.GetMediaUrl(item);
    return uri;
}

2 Answers 2

5

From what I remember, if you add an image from media library into RTE field, Sitecore uses the following format:

<img src="-/media/5EA5C007FFFF4688A0B7E6F58B79D2E8.ashx" />

With your media item id instead of 5EA5C007FFFF4688A0B7E6F58B79D2E8.

Try to use the same format from your code instead of generating the url using MediaManager.

Then the url will be transformed into a user friendly link when the field is generated on your site.


EDIT

You can use MediaUrlOptions.GetShellOptions() to generate url in this format:

using (new Sitecore.SecurityModel.SecurityDisabler())
{
    var item = mediaCreator.CreateFromStream(ms, fileName, options);
    var uri = Sitecore.Resources.Media.MediaManager.GetMediaUrl(item, Sitecore.Resources.Media.MediaUrlOptions.GetShellOptions());
    return uri;
}
3
  • I'm new to Sitecore but this seems counter-intuitive. What's the purpose of MediaManager.GetMediaUrl() if not to generate these URLs for you. Generating the URLs manually seems brittle and clunky. Is there any documentation to support this?
    – Greg B
    Commented Jun 12, 2018 at 8:59
  • Actually you can achieve what I explained with MediaManager. You just need to pass the correct MediaUrlOptions. See edited answer
    – Marek Musielak
    Commented Jun 12, 2018 at 9:20
  • yep. GetShellOptions() works.
    – Greg B
    Commented Jun 12, 2018 at 11:34
3

This happens because your context site is "shell" .

You need to switch to website site using :

   using (new SecurityDisabler())
   {
      var item = mediaCreator.CreateFromStream(ms, fileName, options);
      using (new SiteContextSwitcher(Factory.GetSite("website")))
        {
         var uri = MediaManager.GetMediaUrl(item);
        }
       return uri;
    }
2
  • Actually, the links will be transformed when rendered on the published site. Doing it this way, will prevent the images being editable inside the RTE field. The difference between design time links and runtime links should be made clear.
    – Mark Cassidy
    Commented Jun 12, 2018 at 8:45
  • @MarkCassidy Thanks for the note. The URLs are rendered as is when the site is published and viewed by an anonymous user (i.e. /sitecore/shell/...) . I'm also able to edit the image (change the source) in the RTE after implementing the code Vlad suggests above. Do you have any further information as to why you believe this to be true? Do you have a link to information about design time links? Thanks.
    – Greg B
    Commented Jun 12, 2018 at 9:05

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.