When you build custom pipeline processor in Sitecore, often you need to pass additional configurable parameters, like paths to other resources, or some switches, which will able you to change settings without recompiling the code. This can be done in very easy and clean way by adding those parameters inside the typical pipeline processor node and by using Sitecore Configuration Factory helper:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"> <sitecore> <pipelines> <mvc.requestBegin> <processor type="Sitecore.Feature.Styles.Processors.DynamicStylesProcessor, Sitecore.Feature.Styles" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Request.RequestBegin.SetupPageContext, Sitecore.Mvc']"> <resources hint="list:AddResource"> <resource>/styles/smartsitecore/some.css</resource> <resource>/styles/smartsitecore/other.css</resource> </resources> </processor> </mvc.requestBegin> </pipelines> </sitecore> </configuration> |
Now in our custom pipeline processor code we need “AddResource” method, which will be called for each resource node from config file. By adding a little more code, we can use our additional configuration in “Process” method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using Sitecore.Mvc.Pipelines.Request.RequestBegin; using System.Collections.Generic; namespace Sitecore.Feature.Styles.Processors { public class DynamicStylesProcessor : RequestBeginProcessor { private readonly List<string> _resources = new List<string>(); public void AddResource(string resource) { _resources.Add(resource); } public override void Process(RequestBeginArgs args) { foreach (var resource in _resources) { //do something with resources } } } } |
This method uses Sitecore Configuration Factory generic mechanism, which can be used for other functionalities, not only for pipeline processors.