External aliases

This morning, one of my collegues noticed that you couldn’t use Aliases for external linking. Well… that sounds likea challenge for me :).
The following code is not supported, should work with 5.2 and is extremely unoptimised :P!

using System;
using Sitecore.Pipelines.HttpRequest;
using Sitecore.Data.Items;
using Sitecore.Data.Fields;

namespace LECTRIC.SC.Pipelines
{
class ExternalAliasResolver : AliasResolver
{
public new void Process(HttpRequestArgs args)
{
if (Sitecore.Configuration.Settings.AliasesActive &&
Sitecore.Context.Database != null &&
Sitecore.Context.Database.Aliases.Exists(args.LocalPath))
{
Item aliasItem = Sitecore.Context.Database.GetItem(“/sitecore/system/Aliases” + args.LocalPath);
if(aliasItem != null)
{
LinkField linkField = aliasItem.Fields[“Linked item”];
if (linkField != null && !linkField.IsInternal)
{
System.Web.HttpContext.Current.Response.Redirect(linkField.Url);
}
else
{
base.Process(args);
}
}
}
}
}
}

Getting it to work, you should replace
<processor type=”Sitecore.Pipelines.HttpRequest.AliasResolver, Sitecore.Kernel”/>
with:
<processor type=”LECTRIC.SC.Pipelines.ExternalAliasResolver, LECTRIC.Sitecore”/>
in the httpRequestBegin-pipe line in the Web.config.

Notice that this will decrease the preformance of a request(dnot dramatical, but a little) and that links should be fileld in the right way(‘Insert External link’) in the aliases section. And as you can see, the Alias-folder is hardcoded as it isn’t defined in the web.config. Suggestions for optimising are welcome…

Enjoy!

2 thoughts on “External aliases”

  1. Dear Alex,

    I’ve just used this piece of code for one of our customers and it works fine. 🙂

    You are hereby granted another Sitecore-award.

    Best regards,
    Martijn

  2. Great post, it would be very handy if there were to be a helper method within Item object that you can call to give you the first or a list of aliases assigned to an item. I’ve stumbled across this post as i’m just trying to get the alise URL assigned to the node.

Comments are closed.