How to create a Item Resolver Pipeline in sitecore

By Shekhar Gigras

Two steps required for create the pipelibe and configure it

  1. Create class file and inherit with "HttpRequestProcessor"
  2. Create patch file

Create class file and inherit with "HttpRequestProcessor"

  1. Create library project in visual studio
  2. Add new item as a class
using Sitecore.Pipelines.HttpRequest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace SitecoreUtilityHelper
{
    public class CustomUrl : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            //Handel http://localhost/webapi/getcity or http://localhost/webservice/getstate.ashx because both items not exists in sitecore
            if (Sitecore.Context.Item != null || Sitecore.Context.Database == null || Sitecore.Context.Database.Name == "core"
                || args.Url.ItemPath.Length == 0
                || args.Url.FilePath.ToLower().StartsWith("/webapi") || args.Url.FilePath.ToLower().StartsWith("/webservice"))
                return;

            //if url is change means structure is change in sitecore but old url is already coming in google search or bookmark then how to handel it, in this
            string RequestedURL = HttpContext.Current.Request.Url.ToString();
            if (RequestedURL.ToLower().EndsWith("dell-laptop"))
            {
                //get the right item
                Sitecore.Context.Item = Sitecore.Context.Database.GetItem("<ITEMID>");
            }
        }
    }
}

Create patch file

important for create the patch file is naming connvention of the filename, create the file with the name of z,zz,zzz,zzzz etc..

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <httpRequestBegin>
        <processor type="SitecoreUtilityHelper.CustomUrl,SitecoreUtilityHelper"
       patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']"/>
      </httpRequestBegin>
    </pipelines>
  </sitecore>
</configuration> 

Connect by Whatsapp

Posted in Sitecore on May 23, 2020


Comments

Please sign in to comment!