Pipelines are one of the most essential parts of Sitecore and creating your own custom pipeline in Sitecore makes your code extremely flexible for both you and others.
- It is extremely easy to create and run a custom
pipeline as this post will show.
To add to the existing functionality we could include a new step like
Where we add some custom functions after Step 1 and before Step 2.
We could also replace an existing step completely
Defining the pipeline
- A pipeline consist is a set of processor classes which each has a method called Process which takes one argument of PipelineArgs or a
derived class.
- To configure a pipeline create a new .config XML file in the include folder
- Below, we can see the httpRequestEnd pipeline definition. The three processors are called in the order in which they are listed.
- A parameters object is passed between them to provide continuity. The final processor is also receiving four additional parameters from the config file.
<?xml version="1.0" encoding="utf-8"?>
<sitecore database="SqlServer" xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:security="http://www.sitecore.net/xmlconfig/security/">
…
<pipelines>
…
<httpRequestEnd>
<processor type="Sitecore.Pipelines.PreprocessRequest.CheckIgnoreFlag, Sitecore.Kernel" />
<processor type="Sitecore.Pipelines.HttpRequest.EndDiagnostics, Sitecore.Kernel" role:require="Standalone or ContentManagement" />
<!--<processor type="Sitecore.Pipelines.HttpRequest.ResizePicture, Sitecore.Kernel"/>-->
<processor type="Sitecore.Pipelines.HttpRequest.StopMeasurements, Sitecore.Kernel">
<ShowThresholdWarnings>false</ShowThresholdWarnings>
<TimingThreshold desc="Milliseconds">1000</TimingThreshold>
<ItemThreshold desc="Item count">1000</ItemThreshold>
<MemoryThreshold desc="KB">10000</MemoryThreshold>
</processor>
</httpRequestEnd>
…
</pipelines>
…
</sitecore>
Customize existing Pipelines:
- The first thing to do is to create a configuration patch to add the new processor class into the pipeline at the desired location.
- As you can see from the code it is possible to pass variables to the processor.
- Here we are adding a processor called NewsArticleLogEntryProcessor into the httpRequestBegin pipeline after the ItemResolver
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<httpRequestBegin>
<processor type="Fourbyclub.CustomCode.CustomCode.Pipelines.httpRequestBegin.NewsArticleLogEntryProcessor,Fourbyclub.CustomCode" patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']">
<NewsArticleTemplateID>{B871115E-609F-44BB-91A4-A37F5E881CA6}</NewsArticleTemplateID>
</processor>
</httpRequestBegin>
</pipelines>
</sitecore>
</configuration>
Then we need to create the processor and Inherit the HttpRequestProcessor and Implement the Process method.
All we are doing here is writing to the log if the requested item is a NewsArticle.
namespace Fourbyclub.CustomCode.CustomCode.Pipelines.httpRequestBegin
{
using Sitecore.Pipelines.HttpRequest;
using Sitecore.Diagnostics;
// TODO: \App_Config\include\NewsArticleLogEntryProcessor.config created automatically when creating NewsArticleLogEntryProcessor class.
public class NewsArticleLogEntryProcessor : HttpRequestProcessor
{
// Declare a property of type string:
private string _newsArticleTemplateID;
public string NewsArticleTemplateID { get { return _newsArticleTemplateID; } set { _newsArticleTemplateID = value; } }
public override void Process(HttpRequestArgs args)
{
Assert.ArgumentNotNull(args, "args");
if ((Sitecore.Context.Item != null) && (!string.IsNullOrEmpty(_newsArticleTemplateID)))
{
Assert.IsNotNull(Sitecore.Context.Item, "No item in parameters");
// use util to get id from string property
if (Sitecore.Context.Item.TemplateID == Sitecore.MainUtil.GetID(_newsArticleTemplateID))
{
// view in log file later, so add FourbyclubCustomCode
Log.Info(string.Format("FourbyclubCustomCode: News Article requested is {0} and the item path is {1}", Sitecore.Context.Item.DisplayName, Sitecore.Context.Item.Paths.FullPath), this);
}
}
}
}
}
Create a new Pipleline:
- The first thing to do is to declare the pipeline with a
configuration patch.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<logWriter>
<processor type="Fourbyclub.CustomCode.CustomCode.Pipelines.logWriter.logWriterProcessor,Fourbyclub.CustomCode" />
</logWriter>
</pipelines>
</sitecore>
</configuration>
- The XML above will create a pipeline called logWriter that has a
single processor called logWriterProcessor, which will be in the
Fourbyclub.CustomCode.dll.
- Pipelines must pass a PipelineArgs object to each processor
as it is called so that needs to be defined
using Sitecore.Pipelines;
namespace Fourbyclub.CustomCode.CustomCode.Pipelines.logWriter
{
public class LogWriterPipelineArgs : PipelineArgs
{
public string LogMessage { get; set; }
}
}
- At least one processor is needed to do the work of our pipeline
using Sitecore.Diagnostics;
namespace Fourbyclub.CustomCode.CustomCode.Pipelines.logWriter
{
public class logWriterProcessor
{
public void Process(LogWriterPipelineArgs args)
{
Log.Info(string.Format("FourbyclubCustomCode: The message was {0}", args.LogMessage), this);
}
}
}
- Finally we need to invoke the pipeline in our code somewhere.
- Instantiate the LogWriterPipelineArgs and set the LogMessage.
- Then call CorePipeline.Run and pass it the name of the pipeline and the args.object
var pipelineargs = new LogWriterPipelineArgs();
pipelineargs.LogMessage = "Requested item is not a News Article";
CorePipeline.Run("logWriter", pipelineargs);
This is how simple it is to create a pipeline!!
Comments
Post a Comment