Switching things up with the Sitecore Switcher

Hi everybody!  Have you ever used the SecurityDisabler in Sitecore and thought “that’s kinda cool, I could use something like that”?  Well now you can.  In this fun and informative post I’m going to dive a little bit into the Sitecore.Common.Switcher class.
If you haven’t used the SecurityDisabler, EventDisabler, BulkUpdateContext or their friends before, they are used something like this.
Item item = GetAnItem ();
using (new SecurityDisabler())
  {
  item .Editing . BeginEdit();
  //So something without bothering to check security...
  item ["Title" ] = "I changed your value" ;
  item .Editing . EndEdit();
  }
 //Security is enforced again.
The above example is a fairly simple way to change the behavior of a piece of code.  Being able to create our own methods for doing this will open up a lot of possibilities.
The rest of this post will be a quick demo on how this can be accomplished.  As a quick use case let’s say you have a set of pages that will use the page title field except late on Fridays, when all bets are off.
Somewhere you have the following method:
public static string GetPageTitle (Item item)
           {
                 return DateTime . Now. DayOfWeek == DayOfWeek .Friday && DateTime .Now . Hour > 18 ? "Who cares?" : item ["Page Title" ];
           }
Right now the content author can add a page title and preview the page.  If they want to see what happens on a Friday they’ll have to sit around until 6pm on Friday.  Congratulations, you just lost a client.  What’s a better way to handle this you ask?  By creating a date switcher.
Let’s start by jumping right in a creating a new class that extends Sitecore.Common.Switcher.  We’ll add a new DateTime property and set that in the constructor.  We’ll also call the Enter method, which makes the switch.  In the end it will look something like this:
public class DateSwitcher : Switcher <DateSwitcher >
     {
            public DateTime TheTime { get ; set ; }

            public DateSwitcher () : this( DateTime .Now )
           {
           }

            public DateSwitcher ( DateTime dateTime )
           {
                 TheTime = dateTime;
                 Enter (this );
           }
     }

Our property “TheTime” will store what time that we want to use when rendering our page title.  We’ll need to modify our GetPageTitle to first check to see if the switch is active and then to override the current time if it is.
In the end it will look something like this:

public static string GetPageTitle (Item item)
           {
                 DateTime time = DateTime .Now ;

                 if (DateSwitcher . CurrentValue != null )
                {
                      time = DateSwitcher. CurrentValue .TheTime ;
                }

                 return time . DayOfWeek == DayOfWeek . Friday && time .Hour > 18 ? "Who cares?" : item ["Page Title" ];
           }

We’re now free create a new instance of our DateSwitcher and call the GetPageTitle method.  Keep in mind that the switcher implements the IDisposable interface and uses that for doing some of it’s cleanup.

       using (new DateSwitcher (DateTime . Now. AddDays (3 ) .AddHours ( 1)))
                {
                      //Hello from the future!
                      OutputData = GetPageTitle( item );
                }


A nifty idea would be tie this into a custom editor that the content author’s could use to preview their content under specific conditions.  There’s probably a blog post somewhere about how to set that up…
Switcher01
Switcher02
I hope you found this helpful even if the example was a little watered down.  Feel free to reach out to me if you have any questions.

 

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

%d bloggers like this: