Sitecore Pipelines — The Basics

Rainfall Software
Rainfall Software
Published in
2 min readOct 21, 2022

One of the most important features of Sitecore is the ability for developers to extend and alter just about any behaviour of the application by modifying the pipelines that are run to handle certain actions. In this article, I’m going to go through the process of creating a simple processor that fits into Sitecore’s handling of an incoming HTTP request.

Photo by Quinten de Graaf on Unsplash

Background

The goal of this mini-project is to create a type of item in Sitecore that will appear as a page in the site’s navigation, but redirect to a chosen URL when the link is clicked, rather than opening the page represented.

To do this, we will first create a new template. This template will contain all the fields a normal page would need to be displayed in navigation (possibly inheriting from a base page template) as well as a General Link field name “Redirect URL”.

Definitions

Just to quickly clarify things, there are two definitions we should be aware of:

Pipeline: A set of steps defined by configuration that Sitecore will execute in order during a certain action.

Processor: An individual block of logic within a pipeline. Pipelines are made up of multiple processors.

The Processor

Our processor is going to be part of the pipeline that handles incoming HTTP requests to Sitecore. It will then check for the existence and type of the current item, and if the current page is our redirect template, it will end the request and redirect to the specified URL.

The code for this will looking something like:

public class MyRedirectProcessor : HttpRequestProcessor
{
public override void Process(HttpRequestArgs args)
{
var item = Sitecore.Context.Item;
if (item != null && item.TemplateID == REDIRECT_ID)
{
var redirect = (LinkField)item.Fields["Redirect URL"];
if (!string.IsNullOrWhiteSpace(redirect?.Url))
{
var http = HttpContext.Current;
args.AbortPipeline();
http.Response.RedirectPermanent(redirect.Url);
http.Response.End();
}
}
}
}

Configuration

Just writing the processor class isn’t enough though. We need to add some configuration to tell Sitecore to run this code. The configuration will tell Sitecore which pipeline to add our code to, and also where in the pipeline to add it. This configuration should be added to a patch file, and will be picked up by Sitecore on startup.

<pipelines>
<httpRequestBegin>
<processor type="MyProject.MyRedirectProcessor, MyProject" patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']"/>
</httpRequestBegin>
</pipelines>

This tells Sitecore to add our processor to the httpRequestBegin pipeline, and to add it immediately after the built-in ItemResolver processor.

--

--