Wednesday 2 December 2015

MVC Validation with Sitecore

MVC comes with a validation framework that supports both server-side and client-side validation with very little coding.  You can still use this framework with Sitecore MVC if you wish, but it breaks a few fundamental aspects of content management systems, namely that the error messages you use won't be content editable as they are embedded in your code.  This article shows how we can use the built-in MVC framework and also allow the messages to be content manageable, and also multi-lingual.

Monday 30 November 2015

IDataSourceFilterFactory error when using SwitchMasterToWeb.config

When configuring your content delivery servers you might well be using the SwitchMasterToWeb.config available from Sitecore.  When using this file you might find your site's analytics data stops gathering and that it generates an error like this with every page request;

Exception: System.InvalidCastException
Message: Invalid cast from 'System.String' to 'Sitecore.Analytics.Reporting.Filters.IDataSourceFilterFactory'.

Thursday 29 October 2015

Dynamic placeholders and tabs in Sitecore

The problem


One issue with using placeholders embedded inside Sitecore renderings is that a placeholder can only appear once on the page. If a placeholder with the same name is placed multiple times then Sitecore doesn't know which placeholder is being referenced when it comes to adding the placeholder's controls. If you are using renderings as reusable layouts then this can be restrictive. As an example, let's say your pages have places where you want to show three columns of renderings, and what renderings go inside each component will be decided on a per-page basis. You might create a rendering that looks like this
<div class="columncontainer">
    <div class="col-33">
        @Html.Sitecore().Placeholder("left column")
    </div>
    <div class="col-33">
        @Html.Sitecore().Placeholder("middle column")
    </div>
    <div class="col-33">
        @Html.Sitecore().Placeholder("right column")
    </div>
</div>

and you can then drop that rendering on any page where you want to show three things in columns. Great, but what if you want to show two of these on the same page? Maybe on top of each other giving two rows of three columns. There is nothing stopping you adding two of the renderings to the same page, but when you do you end up with this
<div class="columncontainer">
    <div class="col-33">
        @Html.Sitecore().Placeholder("left column")
    </div>
    <div class="col-33">
        @Html.Sitecore().Placeholder("middle column")
    </div>
    <div class="col-33">
        @Html.Sitecore().Placeholder("right column")
    </div>
</div>
<div class="columncontainer">
    <div class="col-33">
        @Html.Sitecore().Placeholder("left column")
    </div>
    <div class="col-33">
        @Html.Sitecore().Placeholder("middle column")
    </div>
    <div class="col-33">
        @Html.Sitecore().Placeholder("right column")
    </div>
</div>

Now when you tell Sitecore you want to put "Featured product" in the "middle column"...how does Sitecore know which "middle column" you are reffering to? There are two on the page. This limitation can also affect you if you want to do a flexible tab component. Let's say you want a rendering that can show tabbed content and you want the number of tabs to be dynamic. This does have a workaround, when you are creating your tabs you can give each placeholder a unique id based on its index.
<div class="tabs">
    <div class="tab">
        @Html.Sitecore().Placeholder("tab_1")
    </div>
    <div class="tab">
        @Html.Sitecore().Placeholder("tab_2")
    </div>
    <div class="tab">
        @Html.Sitecore().Placeholder("tab_3")
    </div>
</div>

That will work ok, but how do you control what renderings are valid to go inside each placeholder? What renderings can be placed where is configured in the Placeholder Settings section;

/sitecore/layout/Placeholder Settings

Here you create a "Placeholder" item for each placeholder and that item contains the placeholder key and a list of renderings that are valid for that placeholder. If you have placeholders like "tab_1", "tab_2 and "tab_3", then you will need to create 3 Placeholder items, one for each tab, and for each one defining what controls can go in that tab. Not only is this a lot of messy duplication, what happens when someone wants 4 tabs?

Friday 3 July 2015

Customising wrappers

Introduction


When trying to build flexible, generic components that are highly configurable by content editors or designers, sometimes the issue of configurable wrapper element comes up.  For example I might have a text or image field, and I might want to be able to specify if it is wrapped in an h1 tag, or I might want it in an h2 tag, a div with a certain class, or maybe just a p tag will do.  Wrapping elements isn't a problem if your field is defined as a rich text field and you're selecting the styles from the toolbar; this solution is intended for fields that aren't.

Monday 23 March 2015

Creating your own context objects

Context objects


Sitecore provides a number of contexts to allow you to enable and disable certain framework features on a case by case basis. For example if you want to run a piece of code without any security concerns you can use the SecurityDisabler.
using (new SecurityDisabler())
{
    // your code here
}
Any code inside the SecurityDisabler block will ignore all security concerns, so it can create items, edit items, delete items etc without the logged in user having the appropriate permissions. Another common context to use is the BulkUpdateContext. If you are doing a lot of updates to Sitecore items as part of a maintenance routine then putting your code inside a BulkUpdateContext stops Sitecore from recording things in the history engine which will increase the performance of your code.  Similarly the EventDisabler prevents events from being raised.

It's a handy technique that you might want to leverage yourself. In one solution we had caching built at a deep layer, but for certain admin functions we didn't want to use the cache, we always needed the actual data. The solution was to use our own context that could disable caching.

Monday 9 March 2015

Giving content editors control over styles

Introduction

When building a Sitecore solution one of the largest mindset decisions is who the client of your solution is going to be.  Is your site going to be managed by developers?  Or is it going to be managed by the business?  One of the advantages of a CMS is that it gives the business the ability to manage the site themselves, ie your marketing people, your product people and so on, who can run the site without needing help from a developer.  This concept of an end product that can be managed without developer input is of benefit to everyone; the business can instantly do the things they want to do, and developers only need to spend their time developing and not changing META data or page titles.  However many of the Sitecore solutions I get asked to consult on can only realistically be maintained by developers.

There are a few common practices that make for a "developer only" Sitecore solution, but the one I am going to discuss in this blog revolves around configuring styles.  This article assumes you already understand the basics of creating renderings, it's not going to be a complete tutorial on creating Sitecore components.

Friday 27 February 2015

Using Membership with Sitecore

Sitecore's authentication and user management uses asp.net membership with the membership schema in the Core database.  To use authentication in your own sites you can use this membership system yourself, or if you have your own existing membership database you want to use, or you want to implement membership on your site and don't want to use a separate database rather than keeping your users in the Core database, then it is possible to configure Sitecore to implement multiple asp.net membership databases.

Tuesday 3 February 2015

How Sitecore manages icons

Introduction


Any Sitecore developer knows that building a Sitecore solution is 20% writing code and 80% choosing icons.  You might not have given icon management any thought, but not only is it possible to create your own icons for use with Sitecore, but there are already more icons available to you than you think.

Sitecore pipelines in high volume sites

Introduction


If you use class-level variables in your Sitecore pipelines you might discover inconsistent and unwanted behaviour.

Using .net caching with Sitecore

Introduction


Sitecore has an extensive caching framework that allows it to cache everything from items in its database all the way to the html output of its renderings.  When using Sitecore's cache framework it employs a number of techniques to ensure that the cached data remains valid, and if you use .net's own caching framework this can sometimes conflict with Sitecore.  For example, let's say you have a menu control that appears on every page so you use .net's output caching to cache the html.  If a content editor changes something in Sitecore that affects your menu such as creating new elements, renaming titles etc, then your site is going to continue serving up the cached version which is no longer valid.  If you use Sitecore's own caching framework by setting cache parameters on the rendering then Sitecore will clear these cached items when a publish happens, ensuring that the cached html is rebuilt using the new data.  Furthermore, Sitecore will disable caching when you are in preview or edit mode so you know your data is always fresh.

As well as html caching, you might want to cache data structures you have built from Sitecore items.  Let's say you have a list of countries and in each country is a list of cities and you use this data often, so would like to cache it.  If you use .net caching and the underlying data in Sitecore changes, your cached data is now invalid.  You could set an expiry on the cache, but that's far from ideal as you don't want to tell your content editors that they have to wait before they see their changes go live.
In this article I am going to show you how to hook into the publishing mechanism such that your cached items are removed when a publish occurs.