Customization is Crucial
One of the essential customization concepts in Sitecore is the ability to customize the flow of the application globally without having to add a rendering to every page or include code in a universal layout. Creating a custom Sitecore pipeline processor gives developers a way to inject their custom code into the process and it’s relatively easy to do.
A couple of the standard pipelines are Initialize and HttpBeginRequest. Each Pipeline has various Processors in it. For more detailed information on MVC-specific processors and pipelines, you can refer to the Sitecore documentation online. There is additional content on Sitecore Community Github site.
What’s a Pipeline?
In this post, we are not going to deep dive into all of the pipelines and processors. Instead, I will take a few minutes to show how simple it is to create a piece of custom code and inject it into the Sitecore rendering process. In this first example, we are going to demonstrate how to replace the HTML caching headers that Sitecore provides with its HtmlCaching setting and instead inject our custom header values that match our needs.
If we take a look at the Sitecore.config we will find a setting called “DisableBrowserCaching”
<!–
DISABLE BROWSER CACHING
If true, all pages will have:
Cache-Control: no-cache, no-store
Pragma: no-cache
in the http header
–>
<setting name=”DisableBrowserCaching” value=”true”/>
As mentioned in the comment, with this setting set to true Sitecore will set “no-cache, no-store” as the values for the Cache-Control HTTP response header values. It will also supply “private” if the value is false. To add our custom value, we can simply inject our code after Sitecore has done its work. Our code will run after Sitecore and we will get our desired value. For more information on how to get specific on where your code runs, check out the post: Patching Sitecore Processors Tips and Tricks on the Perficient blog.
Creating a Custom Sitecore Pipeline Processor
To create your custom code, create a class and ensure it has an args parameter with a type that matches the type of Processor you are trying to emulate. In this case, I want my code to run in the ActionExecuted pipeline therefore I am using ActionExecutedArgs as my type on the args parameter.
using Sitecore.Mvc.Pipelines.MvcEvents.ActionExecuted;
using System.Configuration;
using System.Web;
namespace MyCode.Pipelines.MvcActionExecuted
{
public class CacheControl
{
public void Process(ActionExecutedArgs args)
{
HttpContext.Current.Response.AddHeader(“Cache-Control”, “my custom values”);
}
}
}
Once you have the code in place you can patch the Sitecore.config with your patch file to inject your code. In this example, I am telling Sitecore that I want my Process function in my custom CacheControl class to run after Sitecore runs its CacheControl processor within the MvcEvents pipeline.
<configuration xmlns:patch=”http://www.sitecore.net/xmlconfig/”>
<sitecore>
<pipelines>
<mvc.actionExecuted>
<processor patch:after=”*[@type=’Sitecore.Mvc.Pipelines.MvcEvents.ActionExecuted.CacheControl, Sitecore.Kernel’]” type=”MyProject.MyCode.Pipelines.MvcActionExecuted.CacheControl, MyProject”/>
</mvc.actionExecuted>
</pipelines>
</sitecore>
</configuration>
What Next?
This is a simple example but hopefully it’s given you some ideas on how you might be able to build a better experience in Sitecore. Once you start to read through the Pipelines you will have a better idea of the capabilities. A few other neat tricks are the ability to run a piece of code every time an item is saved or published, and run cleanup in another area if an item is being deleted. However you decide to apply it, I hope this has been informative and good luck on your custom pipeline adventures!
Leave A Comment