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" />

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google 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 )

Connecting to %s