Tuesday, 4 September 2018

Post-Processing html after it has been generated in Sitecore

Sometimes we might want to do an operation on the entire html generated for a page, such as move elements around, do some text replacements or regex-based url amendments.  The MVC framework has various features like action filters, result filters and so on, but we can't really use these with Sitecore the same way we would a native MVC site.  In traditional MVC your page is the result of a single action, but in Sitecore it is lots of actions stitched together so there is a little more work to do, so let's get started.

Thursday, 30 March 2017

Restricting the number of sub items in Sitecore

In this blog I will be showing a technique that lets us use the Sitecore Rules Engine to restrict how many sub-items an item can have. This restriction is fully configurable and can be based on template types, path locations, item names or any other condition you would like to define.

Tuesday, 28 March 2017

Managing CSS files in Sitecore

This article shows how you can manage CSS files in Sitecore. This is one of those techniques that I don't exactly recommend as it shouldn't really be needed, but sometimes it can be a compromise to allow front-end designers to modify CSS on the fly. One of the advantages of doing this is that having Sitecore drive your CSS means your CSS can also follow the restricted publication process allowing your CSS to change depending on the date. I was working on a site that had a "deal of the day" where each day had a different offer and that offer was reflected on the home page. This technique allowed the designer to version the home page for each day, and also version the css tweaks for each day and set up the site for the whole week in advance. On each morning we simply did a publish and the publish restrictions we'd set up on the versions meant the homepage and css reflected that day's deal.

Note that the css for the majority of the site was still handled in css files, the css that we made driven from Sitecore contained only slight modifications the designer wanted so that buttons looked different, fonts for some components were different and so on.

Saturday, 5 March 2016

Design issues with Sitecore sites

When building a website most companies tend to have designers, or front-end developers, that are fairly hands-on.  Not only do they do the actual site designs but they do the html\css too and sometimes the javascript if needed, or the implementation of any javascript frameworks that are used.  It's also not unusual for this to be the company's first Sitecore solution and all of these things can create their own challenges which I'm going to talk about in this blog.  It's not all technical issues though, it often comes down to politics and people management too.

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?