Setting site specific workflows to items using shared templates in a multisite Sitecore solution

In a multisite solution, we might come across the need to have site specific workflows.
And if we use glassmapper with codegen – we might want to reuse as many templates as we possibly can with the basic goal of reusing as much code as possible.

In the regular scenario, we would set the desired workflow for an item in the ‘Default Workflow’ field of the standard values item of the template. But since we are sharing templates across sites – this doesn’t really work for us!

To resolve this, we came up with a solution – which basically sets the workflow on an item depending on its location in the content tree. We also added an option to turn on / off setting workflow based on the template used to create the item.

We added a field to the system template ‘Template’ which basically indicates if a given template is marked to go into the site workflow or not:


So this field would now be available in all templates – and we could check it for templates, items of which need to be added to the site workflow.

Now to associate a site with a workflow, we added a field to the SiteNode (A parent item of each site in our multisite instance). You could also choose to add this to your home node itself.



So now that we have a way to determine which workflow we need to set on an item based on its location in the content tree and the flag that tells us whether or not it needs to be added to a given workflow, all we need to do is add the event handler which will add the workflow to the item conditionally – when the item is created.

Here’s the code we used in the event handler:

using Sitecore.Configuration;
using Sitecore.Data.Events;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.SecurityModel;
using System;
using System.Configuration;
using Sitecore.Workflows;
using SharedSource.Helpers;
using Event = Sitecore.Events.Event;

namespace shared._Classes.Shared.Customizations.Customized_Sitecore
    public class SetItemWorkflow
        public void OnItemCreated(object sender, EventArgs args)
            using (new SecurityDisabler())
                var arg = Event.ExtractParameter(args, 0) as ItemCreatedEventArgs;
                if (arg == null || (Sitecore.Context.Site != null && Sitecore.Context.Site.Name != "shell")) return;

                    if (!SitecoreHelper.ItemRenderMethods.GetCheckBoxValueByFieldName("Use Site Workflow", arg.Item.Template)) return;

                    Item currentSiteNode = SitecoreHelper.ItemMethods.GetAncestorOrSelfByTemplateId(arg.Item, ConfigurationManager.AppSettings["TemplateGuid_SiteNode"]);
                    if (currentSiteNode == null) return;

                    string currentSiteNodeDefaultWorkflow = SitecoreHelper.ItemRenderMethods.GetRawValueByFieldName("Default Workflow", currentSiteNode, false);

                    if (string.IsNullOrWhiteSpace(currentSiteNodeDefaultWorkflow)) return;

                    using (new EditContext(arg.Item, SecurityCheck.Disable))
                        IWorkflow workflow = Factory.GetDatabase("master").WorkflowProvider.GetWorkflow(currentSiteNodeDefaultWorkflow);

                        if (workflow == null) return;
                catch (Exception ex)
                    Log.Error("Error on creating item (SetItemWorkflow):" + arg.Item.ID + Environment.NewLine + "StackTrace:" + ex.StackTrace, ex);

Some of the helper methods used here:

  • GetCheckBoxValueByFieldName: Gets the value from a checkbox field and casts it into a boolean value
  • GetAncestorOrSelfByTemplateId: Recursively goes up the content tree from the current item upto the content node, and returns the first item matched by template id passed in
  • GetRawValueByFieldName: Gets the raw value stored in the given field

Additionally, we add the configuration required to add this event handler to the item:created event:

      <event name="item:created">
        <handler type="shared._Classes.Shared.Customizations.Customized_Sitecore.SetItemWorkflow, shared" method="OnItemCreated" />

SUG Bangalore August 2015 Meetup

We had an interesting session this time which was relevant to Sitecore sites hosted on CDN servers. The presenters from Sapient – Jai Prakash & Vinod Bhasyam displayed their ample experience in dealing with Sitecore and CDN hosting.

We learnt about the solution that they came up with in dealing with personalization for Sitecore sites which were cached on CDN servers. Since we at Verndale were quite new to the concept, it was a great learning for us. Right from understanding the basics of hosting on CDN (Cloud Delivery Networks) to implementing work arounds to get Personalization working as expected – to which CDN hosting brings up unique challenges.

(Please excuse the low sound for first 10 minutes!)

This was a great opportunity for us to network and socialize too! 🙂 Here are some pictures!