What is a Pipeline?
- A pipeline is basically a method whose flow is defined using XML.
- A pipeline consists of a sequence of processors. A processor is a .NET class that implements a method. When a pipeline is invoked, the processors are run in order.
What are Custom Processors?
While there is neither a class to extend nor an interface to implement, a convention must be followed in order for Sitecore to be in order for a class to be used as a processor:
- If the processor has a method specified, the method must accept a PipelineArgs object and return void.
- If the processor does not have a method specified, the processor must have a method named Process which accepts a PipelineArgs object and returns void.
The following is an example of a custom processor.
public class SetVal1
{
public void Process(MyPipelineArgs args)
{
Sitecore.Diagnostics.Assert.ArgumentNotNull(args, "args");
//do something
}
}
Creating a Custom Pipeline
Creating custom pipelines is an essential part of integrating an external system with Sitecore.
- PipelineArgs
- Defining a Pipeline
- Invoking a Pipeline
PipelineArgs
When creating a custom pipeline a custom PipelineArgs class is not required. The standard PipelineArgs class can be used.
However, a custom PipelineArgs class makes it easier to pass objects between processors and to provide output to the process called the pipeline. At runtime, the PipelineArgs object acts as the pipeline’s context.
In order to create a custom PipelineArgs class, you must inherit from Sitecore.Pipeline.PipelineArgs.
public class MyPipelineArgs : Sitecore.Pipelines.PipelineArgs
{
public string Result { get; set; }
}
Defining a Pipeline
A pipeline itself is nothing more than a block of XML in the configuration > Sitecore > pipelines section of Web.config or a Sitecore patch file.
The following is an example of a custom pipeline definition.
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<myPipeline>
<processor type="SitecoreLearning.Helpers.CustomProcessor.SomeProcessor, SitecoreLearning" />
</myPipeline>
</pipelines>
</sitecore>
</configuration>
Invoking a Pipeline
Create the processor class(es):
public class SomeProcessor
{
public void Process(MyPipelineArgs args)
{
args.Result = GetResult(); // Write a bussines logic here what are you want to implment.
}
private string GetResult()
{
return "Hello custom pipeline world";
}
}
To call the pipeline we use the CorePipeline.Run method as follows:
var pipelineArgs = new MyPipelineArgs();
Sitecore.Pipelines.CorePipeline.Run("myPipeline", pipelineArgs); // Invoking a Pipeline
Log.Info(pipelineArgs.Result, this);
This is how simple it is to create a custom pipeline.
No comments:
Post a Comment