Pipelines – remember the big picture

Sitecore pipelines are great. With them you can relatively easily add and remove functionality as you wish. Pipelines like httpRequestBegin, httpRequestProcessed and mvc.beginRequest are also really useful if you need some logic to run on a page load that shouldn’t really be part of a rendering. This could be anything from login checks to updating the way 404 pages are returned. However you do need to remember the big picture of what you are changing.

Pipelines don’t just effect the processes of the website your building, Sitecore uses them too. That means when you add a new processor to the httpRequestBegin pipeline, that’s going to effect every request in the admin cms too. Just checking the context item also isn’t enough as some things. e.g. opening a node in a treeview, will have the context of the node you clicked on!

Adding this snippet of code to the begining of your process should keep you safe though.

using Sitecore.Diagnostics;
using Sitecore.Pipelines.HttpRequest;
using Sitecore;
using Sitecore.SecurityModel;

namespace CustomPiplelineNamespace
{
    public class CustomPipeline : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            //Check args isn't null
            Assert.ArgumentNotNull(args, "args");
            //Check we have a site
            if (Context.Site == null)
                return;
            //Check the sites domain isn't one for sitecore
            if (Context.Site.Domain != DomainManager.GetDomain("sitecore"))
                return;
            //Check that we're not in a redirect
            if (args.Url.FilePathWithQueryString.ToUpperInvariant().Contains("redirected=true".ToUpperInvariant()))
                return;
            //Check that we're not in the page editor
            if (Context.PageMode.IsPageEditor)
                return;

            // DO CODE
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s