13

I have an item in the media library in Sitecore and I want to place a link to that item on the site. How do I properly get the link for that media item?

15

This will also fold in protection for the media request:

var theURL = Sitecore.Resources.Media.MediaManager.GetMediaUrl(mediaItem);
mediaUrl = Sitecore.Resources.Media.HashingUtils.ProtectAssetUrl(theURL);

Here is the link to Sitecore documentation that explains about protecting Sitecore media from abuse - https://doc.sitecore.net/sitecore_experience_platform/setting_up__maintaining/security_hardening/configuring/protect_media_request

18

Getting the URL of a MediaItem is pretty straightforward, you pass the MediaItem reference to Sitecore.Resources.Media.MediaManager.GetMediaUrl:

var url = MediaManager.GetMediaUrl(mediaItem);

GetMediaUrl takes an optional second parameter of Sitecore.Resources.Media.MediaUrlOptions, which can be used to add query string parameters to the URL which are useful for a number of dynamic image generation features.

Some of the settings that can be changed are:

  • height
  • width
  • max height
  • max width
  • background color
  • aspect ratio mode
  • scale
var options = new MediaUrlOptions { Width = 400, Height = 300 };
var resizedUrl = MediaManager.GetMediaUrl(mediaItem, options);

If you're using Sitecore 8 or newer, Sitecore has a built-in check to prevent malicious users from requesting lots of image resizing on the server and DDOSing your site.

To make use of this new utility, pass the url returned from MediaManager.GetMediaUrl to Sitecore.Resources.Media.HashingUtils.ProtectAssetUrl:

var options = new MediaUrlOptions { ... };
var unsafeUrl = MediaManager.GetMediaUrl(mediaItem, options);
var url = HashingUtils.ProtectAssetUrl(unsafeUrl);

Lastly, Sitecore's url generation will often include spaces that aren't properly percent encoded. This will cause issues in particular situations, such as assigning a Sitecore url to the [content] attribute of <meta property="og:image" content="..." />.

I haven't seen issues with other characters, so the simplest solution I've found is to just use String.Replace:

var url =
    MediaManager
        .GetMediaUrl(mediaItem /*, options */)
        .Replace(" ", "%20");

Sitecore does have a large number of encoding bugs, so I wouldn't be surprised if other special characters were found that also needed to be percent encoded.


For convenience, I use an extension method for getting MediaItem urls:

public static class MediaItemExtensions
{
  public static string GetUrl(this MediaItem item, MediaUrlOptions options = null)
  {
    if (item == null)
      throw new ArgumentNullException("item");

    if (options == null)
      options = new MediaUrlOptions();

    var url = MediaManager.GetMediaUrl(item, options);

#if SITECORE8
    url = HashingUtils.ProtectAssetUrl(url);
#endif

    url = url.Replace(" ", "%20");

    return url;
  }
}

which can be used as:

var url = mediaItem.GetUrl(/* options */);
5

Use this:

Sitecore.Data.Fields.ImageField myImageItem = (Sitecore.Data.Fields.ImageField)Sitecore.Context.Item.Fields["MyImageField"];

then:

Sitecore.Resources.Media.MediaManager.GetMediaUrl(myImageItem.MediaItem);
4

MediaManager has a GetMediaUrl method that can be used for this:

Sitecore.Resources.Media.MediaManager.GetMediaUrl(MediaItem item);

Sample usage:

string mediaUrl = Sitecore.StringUtil.EnsurePrefix( '/',
  Sitecore.Resources.Media.MediaManager.GetMediaUrl(mediaItem));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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