Custom Item Url and resolving the item in Sitecore - Buckets

I have a situation to update the Item URL to be SEO friendly as my items in content tree looks like below.



In the above image in order to resolve the item, Sitecore will find the item from the below URL http://domainname/en/dine/nv/chicken
but I need to form a URL that is more SEO friendly like http://domainname/en/chicken

In Sitecore by Out of box, we can achieve this by using Aliases, we can create an Aliases under SystemàAliases and in the Linked Item field select the target item.

But this is very tedious as we need to go to every item in the content tree and create Aliases, instead of that we will be going to do it programmatically using custom pipelines.

  • ·         Link Provider (Provides the custom URL for the item based on the templates)
  • ·         Item Resolver (Resolves the item based on the custom URL generated by CustomLinkProvider, that we going to create later. Sitecore could not resolve the item in the specified path in the content tree from the given URL path /chicken as item is present only under the following path /dine/nv/chicken )


Create Link Provider:
/// <summary>
/// This class is used to create custom URL for the item based on the particular template
/// </summary>
public class CustomLinkManager: LinkProvider
{
        public override string GetItemUrl(Sitecore.Data.Items.Item item,
        UrlOptions options)
        {
            //Only update the url's for the template items
            if (item.TemplateID == ID.Parse("{AAECA0F0-87E7-42F1-9278-24C2C9250A06}"))
            {
               //forming the url http://domainname/en/chicken
                               //item.Name=chicken
                return FileUtil.MakePath(item.Name, string.Empty).Replace(" ", "-");
            }
            return Sitecore.StringUtil.EnsurePostfix('/',
            base.GetItemUrl(item, options));
        }
 }

Create ItemResolver:

/// <summary>
/// This method used to resolve the item in sitecore using index based on the item name
/// </summary>
/// <param name="args"></param>
 public override void Process(HttpRequestArgs args)
 {
    if (Context.Item != null || Context.Database == null || args.Url.ItemPath.Length ==0)
       return;

            //http://domainname/en/chicken/
            var requestUrl = args.Url.ItemPath.TrimEnd('/');

            //http://domainname/en/chicken
            var index = requestUrl.LastIndexOf('/');
           //Gets the item name "chicken"
            var itemName = requestUrl.Substring(index + 1);
            using (var searchContext =                ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext())
            {
                //Find the item from the index using name and template id
                ID itemTemplateID = new Sitecore.Data.ID(new Guid("{AAECA0F0-87E7-42F1-9278-24C2C9250A06}"));
                var result = searchContext.GetQueryable<SearchResultItem>().
                    Where(
                        x => x.TemplateId == itemTemplateID &&
                        x.Name == itemName
                    ).FirstOrDefault();

                if (result != null)
                {
                    var item = result.GetItem();
                    if (item.Language == Context.Language)
                    {
                        Context.Item = result.GetItem();
                    }
                }
                

            }   
 }

Config Update:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <httpRequestBegin>
        <processor   patch:before="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']" type="xxxxxxxx.CustomItemResolver,xxxxxxxx" />
      </httpRequestBegin>
    </pipelines>
    <linkManager>
      <providers>
        <add name="sitecore">
          <patch:attribute name="type">xxxxxxx.CustomLinkManager,xxxxxxxx</patch:attribute>
        </add>
      </providers>
    </linkManager>
  </sitecore>
</configuration>


The same process can be applied to resolve the Item for the Bucket items in Sitecore.

Comments

Popular posts from this blog

Exploring Sitecore XM Cloud Forms: A Comprehensive Overview

Sitecore Custom Rule (Action and Condition)