Switch the Context Item

Some times you’ll come across some code which relies on reading the Sitecore.Context.Item and there is no way for you to change this. This can occur when calling some standard Sitecore pipelines, some custom code which has been inherited without the source etc. Well, Sitecore API to the rescue. Introducing the ContextItemSwitcher I am not sure how long this has been a part of the Sitecore API, but at least since 6.4 where I first noticed it. ...

November 7, 2013 · 1 min · alc

Sitecore Analytics: Display content by relevance using profile cards

For this post to make sense I will need to create a user story. I will not try to create a standard scenario instead I will create a scenario that is easy to understand and easily applicable to this post. The problem we want to solve is to present some content to the user that is relevant to the content that the user is looking at in any given time. ...

November 4, 2013 · 3 min · ap

Ensuring roles in Sitecore

Roles are easy to create in Sitecore but sometimes you might want to ensure that some specific roles always exists. Not a very common scenario but nonetheless it happens. Once upon a time we needed to be able to ensure that a long list of specific roles always existed on the production instance otherwise some of our code and an integration to an external system could fail. Inspired by the EnsureAnonymousUsers processor in the initialize pipeline I made the following small module. It is very similar to the code I shown in my previous post on how to disable the admin user. ...

October 30, 2013 · 2 min · alc

Lucene: Indexing DateTime from Sitecore and querying date ranges

The title should be self explanatory. I need to index some sitecore items that represent some entities that are valid to the public between certain dates. The sitecore fields are date time fields looking like this. I needed a solution for indexing these values in a way that i can do range queries on them. I used the UnixTimeStamp for converting the DateTime field in a UnixTimeStamp field. public static string ConvertToUnixTimeStamp(DateTime value) { var date = new DateTime(1970, 1, 1, 0, 0, 0, value.Kind); var unixTimestamp = Convert.ToInt64((value - date).TotalSeconds); return unixTimestamp.ToString(CultureInfo.InvariantCulture); } Decoding UnixTimeStamp to DateTime : ...

October 29, 2013 · 1 min · ap

Creating a custom pipeline in Sitecore

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. 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. ...

October 25, 2013 · 1 min · alc

Preview deletes the .aspxauth cookie

I’ve been working a bit on an extremely weird support case the last couple of days. When the editors on a site clicked on preview from either the content editor or the page editor then the page ended up in a redirect loop. Who stole my cookie? Using Fiddler I quickly noticed that the .ASPXAUTH cookie was removed from the session as soon as preview was clicked and what actually happened was the user being redirected infinitely to the login page until the browser crashed (chrome crashes nice, IE just fails miserably). ...

October 23, 2013 · 2 min · alc

Creating a customHandler in Sitecore

Sometimes you would like all requests to a specific path and below to be handled by a specific generic handler. For example in the same manner as the mediaHandler works in Sitecore. Setting up a customHandler in Sitecore Configuring a normal httpHandler in .NET is easy, you simply add entries in the web.config depending on your version of IIS. Under /configuration/system.webServer/handlers ( for IIS 7 and above) <add verb="*" path="examplehandler.ashx" type="[NAMESPACE].ExampleHandler, [ASSEMBLY]" name="ExampleHandler" /> and the same under /configuration/system.web/httpHandlers (for older versions of IIS) ...

October 22, 2013 · 2 min · alc

How to hijack Sitecore instance using only cookie information

Or how to scare any project manager, sales guy or customer into choosing to run their site on https. This is not going to be a lesson in how to obtain cookie information sent over a network. You can find a ton of youtube videos and other resources on how to setup a tool like cain and abel to do this in minutes. This post is not really about Sitecore either, the example just shows a Sitecore site, ...

October 17, 2013 · 4 min · alc

A note about Sitecore pipelines

My two previous posts both contained implementations of custom processors for the httpRequestBegin pipeline. Just a quick explanation about their structure and some absolute best practices when implementing your own Sitecore pipeline processors. Check and exit fast Since the httpRequestBegin pipeline handles all requests it is extremely important to check the context for validity before continuing processing. If a simple check fails then exit the processor right away. Example public class MyCustomProcessor { public void Process(PipelineArgs args) { if (!IsContextValidForProcessor()) return; DoSomeOperation(); } } This rule applies for all pipeline processors, if the context is not valid for the operation exit immediately and let the rest of the pipeline continue on. ...

October 13, 2013 · 2 min · alc

HTTPS in Sitecore

I would always recommend running all production site cms’ using security on the transport layer (https). Sitecore or no Sitecore, it is not safe to first send username and password unencrypted and then following having an insecure session cookie for the authentication information. This applies for both extranet users and the backend administrators. A basic best practice is simply to ensure that all requests containing authentication information such as the .ASPXAUTH cookie used by ASP.NET, only gets transferred using the https scheme. ...

October 13, 2013 · 4 min · alc