Home » Aliases » Resolve Media Library Items Linked in Sitecore Aliases

Resolve Media Library Items Linked in Sitecore Aliases

Sitecore Technology MVP 2016
Sitecore MVP 2015
Sitecore MVP 2014

Enter your email address to follow this blog and receive notifications of new posts by email.

Tonight I was doing research on extending the aliases feature in Sitecore, and discovered media library items linked in them are not served correctly “out of the box”:


As an enhancement, I wrote the following HttpRequestProcessor subclass to be used in the httpRequestBegin pipeline:

using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Pipelines.HttpRequest;
using Sitecore.Resources.Media;
using Sitecore.Web;

namespace Sitecore.Sandbox.Pipelines.HttpRequest
    public class MediaAliasResolver : HttpRequestProcessor
        public override void Process(HttpRequestArgs args)
            Assert.ArgumentNotNull(args, "args");
            if (!CanProcessAliases())

            string mediaUrl = GetMediaAliasTargetUrl(args);
            if (string.IsNullOrWhiteSpace(mediaUrl))

            Context.Page.FilePath = mediaUrl;

        private static bool CanProcessAliases()
            return Settings.AliasesActive && Context.Database != null;

        private static string GetMediaAliasTargetUrl(HttpRequestArgs args)
            Assert.ArgumentNotNull(args, "args");
            ID targetID = Context.Database.Aliases.GetTargetID(args.LocalPath);
            if (targetID.IsNull)
                return string.Empty;

            Item targetItem = args.GetItem(targetID);
            if (targetItem == null || !IsMediaItem(targetItem))
                return string.Empty;

            return GetAbsoluteMediaUrl(targetItem);

        private static bool IsMediaItem(Item item)
            Assert.ArgumentNotNull(item, "item");
            return item.Paths.IsMediaItem && item.TemplateID != TemplateIDs.MediaFolder;

        private static string GetAbsoluteMediaUrl(MediaItem mediaItem)
            string relativeUrl = MediaManager.GetMediaUrl(mediaItem);
            return WebUtil.GetFullUrl(relativeUrl);

The HttpRequestProcessor subclass above — after ascertaining the aliases feature is turned on, and the item linked in the requested alias is a media library item — gets the absolute URL for the media library item, and sets it on the FilePath property of the Sitecore.Context.Page instance — this is exactly how the “out of the box” Sitecore.Pipelines.HttpRequest.AliasResolver handles external URLs — and passes along the HttpRequestArgs instance.

I then wedged the HttpRequestProcessor subclass above into the httpRequestBegin pipeline directly before the Sitecore.Pipelines.HttpRequest.AliasResolver:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
        <processor patch:before="processor[@type='Sitecore.Pipelines.HttpRequest.AliasResolver, Sitecore.Kernel']"
                   type="Sitecore.Sandbox.Pipelines.HttpRequest.MediaAliasResolver, Sitecore.Sandbox" />

Let’s take this for a spin.

I had already defined the following alias in Sitecore beforehand — the error page at the top of this post is evidence of that:


After navigating to http://sandbox/pizza — the URL to the pizza alias in my local Sitecore sandbox instance (don’t click on this link because it won’t go anywhere unless you have a website named sandbox running on your local machine) — I was brought to the media library image on the front-end:


If you have any recommendations on improving this, or further thoughts on using aliases in Sitecore, please share in a comment below.

Until next time, have a pizzalicious day!



  1. dsolovay says:

    What would be a business case for creating aliases for media library items? Usually media assets, even when they stand alone, are presented in an HTML frame. Wouldn’t serving them on their own interfere with expected CMS functionality like page tracking and profiling?

    • Hey Dan,

      I could see someone wanting to serve up an infographic using a media library item alias, though that might be a stretch.

      However, I saw the opportunity to patch a hole — without the processor above, you get an error which isn’t cool — and decided to run with it.

      Regarding tracking, couldn’t one extend Sitecore.Resources.Media.MediaRequestHandler, and execute DMS API code in there? This sounds like an awesome topic for a future blog post! 😉

    • Lars Nielsen says:

      There are few cases.

      For example, a new version of the Sitecore Gartner report and the case where our engagement plan as a service (and to re-engage) notify those who have previous downloaded there report using emails (personalized).

      Instead of passing the recipient to a landing page, – they have already downloaded previously, you could include a (friendly, personalized) link.

      Question is, Mike, will this work with personalized emails (probably), and even better, what if the entire aliased link included the users personalized link.

      • Hi Lars,

        I am uncertain whether this would work with media library item aliases set in HTML image tags in emails. My suspicion would be that it would not work but I will test this out, and report back.


      • Hi Lars,

        I successfully sent an email to myself with an html image tag pointing to the pizza alias I used in this post — the image rendered for me in my test email.

        I only tested this in gmail but suspect it would work in other email clients as well.

        I believe the media library alias URL could be personalized, though haven’t tested this.


  2. […] idea for this post originated from a comment by Sitecore MVP Dan Solovay on my post covering how to resolve media library item […]


Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: