Sitecore “Switchers”

Sitecore has tons of “Switchers” that are pretty useful in many scenarios. You’re probably familiar with a few of them, such as EditContext, SecurityDisabler and LanguageSwitcher. Others are less known but very valuable when needed.

Switchers temporary puts Sitecore in a different mode for a piece of code and then restores it to its previous mode when done. A typical usage would look something like this:

using (new LanguageSwitcher(lang)) {
  // perform custom operations on "lang" instead of the context language

I made a list of common, and not so common switchers. Hope you find some of them useful.

Switches to a different language than the current context language. Handy when working with items as many API calls uses the context language instead of the item language being processed.

Switches to a different database than the current context database. Handy when working with background jobs etc., where the context database often is core.

SecurityDisabler / SecurityEnabler
Disabled/Enables security checking when working on items etc. Useful when creating commands etc. that can be executed by authors that normally don’t have read/write access to the affected items.

UserSwitcher / DomainSwitcher / ProfileSwitcher
Enables impersonation of code, so that code can run as a specific user. Useful when security needs checking or when item statistics and audit trails etc. are important.

Replaces the current context item.

SiteContextSwitcher / DeviceSwitcher
Switches current site and device. This can be very useful when for example rendering links etc. outside a page request.

Disables updates of the Links Database. This may speed up item saves when doing changes to items from code that you know won’t change any links.

This one has become one of my favorites. When working with many items in background tasks etc., it will use the regular Items cache for fetching items, but on cache misses, it won’t put items loaded from the database into the items cache. This avoids the items cache from being polluted by for example a background task traversing multiple items. As the number of items grows in a solution, it’s important that the items being accessed by users are in the cache and aren’t outcompeted by one-time operations etc.

My understanding is that this one is pretty much the same as CacheWriteDisabler, but works more directly in the ItemCache and PathCache.

On a site you have the option to enable version presence, meaning that an item without a version would give a 404. If this is enabled, you might still need to work with items from code that do not yet have a version. This switcher disables that checking, so that you can work with the item despite not (yet) having a version.

Disables events from being fired. Use with great care! It disables all events including cache evictions, index updates etc.

Disables the item locking mechanism. The __lock field isn’t updated and the locking events aren’t fired.

Enables switching of UI culture in the current running thread.

Other “disposables”

There are some other classes that isn’t “switchers” as such, but works in a similar way:

Puts an item into editing mode, so you can edit it. The item is persisted when disposed. It has its drawbacks, so consider using BeginEdit/EndEdit instead within a try/finally block.

So what’s your favorite switcher?