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?
4 Answers
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
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 */);
Use this:
Sitecore.Data.Fields.ImageField myImageItem = (Sitecore.Data.Fields.ImageField)Sitecore.Context.Item.Fields["MyImageField"];
then:
Sitecore.Resources.Media.MediaManager.GetMediaUrl(myImageItem.MediaItem);
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));
-
A complete answer should include GetAssertUrlHash() these days. Ref: kirkegaard-at.blogspot.ch/2015/06/media-hash-and-resizing.html– Mark Cassidy ♦Commented Oct 12, 2016 at 22:11