3

We have created an alias item from /sitecore/templates/System/Alias. We need to use different url for different languages. To do this we have made "Linked item" item not shared. but it is picking the link from English version item only even for other languages also.

Can you please suggest what can be the possible reason and solution?

4

You need to extend Sitecore.Data.AliasResolver class.

You need to modify just this method in this class:

public virtual LinkField this[string alias]
    {
        get
        {
            Error.AssertString(alias, "alias", true);
            if (alias.Length > 0)
            {
                Item item = ItemManager.GetItem(FileUtil.MakePath("/sitecore/system/aliases", alias, '/'), Language.Invariant, Version.First, this.m_database, SecurityCheck.Disable);
                if (item != null)
                {
                    return item.Fields["linked item"];
                }
            }
            return null;
        }
    }

Please modify this line :

 Item item = ItemManager.GetItem(FileUtil.MakePath("/sitecore/system/aliases", alias, '/'), Sitecore.Context.Language, Version.First, this.m_database, SecurityCheck.Disable);

After you need to extend Sitecore.Pipelines.HttpRequest.AliasResolver . You need to override entire class because it has ProcessItem and ProcessExternalUrl private methods. In this methods you need to use your custom alias resolver not the default Sitecore AliasResolver class.

| improve this answer | |
4

Here is an explanation.

Reason:

'Linked item' field of the 'Alias' template was designed to be shared in all languages. For this, the invariant language is used to retrieve alias items.

To make the 'Linked item' field unshared (versioned or unversioned) requires some changes in AliasResolver pipeline in addition to make it not shared.

To make changes, you need to execute following steps to resolve your issue:

  1. Create your own implementation of AliasReolver pipeline that handles the issue of invariant language.

Following is the default implementation of ProcessItem.

private bool ProcessItem(HttpRequestArgs args)
        {
            ID targetID = Context.Database.Aliases.GetTargetID(args.LocalPath);
            if (!targetID.IsNull)
            {
                Item item = args.GetItem(targetID);
                if (item != null)
                {
                    this.ProcessItem(args, item);
                }
                return true;
            }
            Tracer.Error("An alias for \"" + args.LocalPath + "\" exists, but points to a non-existing item.");
            return false;
        }

You need to change the implementation to get the right targetID based on language.

For reference, I have modified default implementation to an extent, I have copied the complete default implementation of AliasResolver and changed ProcessItem to get right language item version. You may need to alter this implementation according to your alias path or better way. It is an idea as we have also encountered this problem in past.

    using Sitecore;
    using Sitecore.Configuration;
    using Sitecore.Data;
    using Sitecore.Data.Items;
    using Sitecore.Diagnostics;
    using System;
    using Sitecore.Data.Fields;
    using Sitecore.Data.Managers;
    using Sitecore.Globalization;
    using Sitecore.IO;
    using Sitecore.SecurityModel;
    using Sitecore.Web;
    using System.Reflection;

public class AliasResolver : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            if (!Settings.AliasesActive)
            {
                Tracer.Warning("Aliases are not active.");
            }
            else
            {
                Database database = Context.Database;
                if (database == null)
                {
                    Tracer.Warning("There is no context database in AliasResover.");
                }
                else
                {
                    Profiler.StartOperation("Resolve alias.");
                    if (database.Aliases.Exists(args.LocalPath) && !this.ProcessItem(args))
                    {
                        this.ProcessExternalUrl(args);
                    }
                    Profiler.EndOperation();
                }
            }
        }

        private void ProcessExternalUrl(HttpRequestArgs args)
        {
            string targetUrl = Context.Database.Aliases.GetTargetUrl(args.LocalPath);
            if (targetUrl.Length > 0)
            {
                this.ProcessExternalUrl(targetUrl);
            }
        }

        private void ProcessExternalUrl(string path)
        {
            if (Context.Page.FilePath.Length <= 0)
            {
                Context.Page.FilePath = path;
            }
        }

        private bool ProcessItem(HttpRequestArgs args)
        {
            //ID targetID = Context.Database.Aliases.GetTargetID(args.LocalPath);

            Item item = ItemManager.GetItem(FileUtil.MakePath("/sitecore/system/aliases", args.LocalPath, '/'), Sitecore.Context.Language, Sitecore.Data.Version.First, 

Context.Database, SecurityCheck.Disable);

            LinkField field = item.Fields["Linked Item"];
            ID targetID = field.TargetItem.ID;

            if (!targetID.IsNull)
            {
                Item target = args.GetItem(targetID);
                if (target != null)
                {
                    this.ProcessItem(args, target);
                }
                return true;
            }
            Tracer.Error("An alias for \"" + args.LocalPath + "\" exists, but points to a non-existing item.");
            return false;
        }

        private void ProcessItem(HttpRequestArgs args, Item target)
        {
            if (Context.Item == null)
            {
                Context.Item = target;
                Tracer.Info(string.Concat(new object[] { "Using alias for \"", args.LocalPath, "\" which points to \"", target.ID, "\"" }));
            }
        }
    }
  1. Patch your class with default one and insert a new line that reference your custom AliasResolver.

Hope it works for you.

| improve this answer | |
0

Please take the following steps:

  1. Create the item in specific language in which you want to point through the aliases
  2. Also create the alias in the specific language also. By default it will get created in en-US but please create in specific language instead of all English language
| improve this answer | |

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.