Sitecore Insiders

SXA and Workflows

How to apply workflows to SXA items? Here is my research and.. a solution.

Options

After reading some blogs and StackOverflow posts I thought I had the right answer after some minutes, in sum, I have found 3 possible approaches:

  • The recommended
    • How:
      • Create a new template, define the workflow, inherit the target SXA template and set it on “Datasource Configurations”, now repeat it for each used SXA template in your site.
      • Tip: For multi-site projects with different workflows, store those templates in Project layer inside a folder with the site name .
    • Pros:
      • Done as recommended .
      • In case you have multiple sites, you can set different workflows for each site .
    • Cons:
      • Maintenance not that easy as the option below.
  • The acceptable
    • How:
      • Use rules system based on the one made by MasterToWeb, where you define a rule for item creation and apply the workflow according to the rule defined.
      • Then you run a Powershell script for the already existing items, defining the “Default Workflow”. As soon as an item is edited it will start the workflow.
    • Pros:
      • Less templates .
      • In case of SXA upgrades with new templates, you wouldn’t need to be concerned about creating compatible templates.
      • Error proneness, defining rules to apply workflow to /Home and /Data descendants guarantees that everything is under the workflow.
      • You can define different workflows for same template and for same site. If you have in the same site, some areas being work by different teams working with different workflows, this is the solution I would say.
    • Cons:
      • Not that performant because instead of having the Default Workflow field defined on a single template and a single time, you will have all your items with that field with that value.
      • In order to change the workflow you will need to Powershell again.
      • Powershell based strategy, in case you have a production Content Management instance open to public, you will need to find another way.
  • The really not recommended
    • How:
      • Set the workflow in a template, add the template to the base templates of all the SXA templates you need.
    • Pros:
      • Compared with the other options, none
    • Cons:
      • Not recommended
      • Not upgradable (you can upgrade SXA but you will lose those changes) Limited to one workflow for an SXA component

The acceptable option

First step

I’ve based my solution on the one made by MasterToWeb, I gave it a very little change on the way it was applying the workflow based on Sitecore documentation, because you should just apply the workflow to the field “Default Workflow”.

Here is the description of the workflow fields according to Sitecore – Understanding Workflows

The Workflow section consists of four fields. Below is their brief description.

Workflow – the current Item’s workflow. Blank if the Item is not currently in a workflow. This field should be blank in Templates and Masters. Sitecore automatically sets this field based on the Default Workflow (see below) and the Workflow commands that the User selects.

State – the current Item’s workflow state. Blank if the Item is not currently in a workflow. This field should be blank in Templates and Masters. Sitecore automatically sets this field based on the Default Workflow (see below) and the Workflow commands that the User selects.

Lock – indicates whether the current Item is locked (checked out or in) and by whom. Sitecore automatically sets this field.

Default Workflow – the workflow that the Item enters when created or edited. If blank on an Item, Sitecore will use the value in the Item’s Template. If blank on the Template, the Item will not be placed in a workflow.

Second step

The second part of the job is to run a Powershell script and this brings some questions… Should the items be in the final state? Or should we leave them empty and wait for an edit?

The base is the Default Workflow, that field must be set, the others will depend on the strategy.

  • If all the items are already published, leave the fields empty, as soon as someone edit the item it will apply the workflow.
  • Otherwise, define the Workflow and Workflow State fields .
  • Option C, tests were done, things gone wrong, let’s reset fields.

Option C Script (covers all the cases, it’s just a matter of changing the code a bit)

#Change this to your workflow item ID function 
$script:workflowID = "{00000000-0000-0000-0000-000000000000}" 

SetWorkflowAndState([Sitecore.Data.Items.Item]$contentItem)

{

$contentItem | Reset-ItemField -Name "__Workflow" -IncludeStandardFields $contentItem | Reset-ItemField -Name "__Workflow State" -IncludeStandardFields    $contentItem."__Default Workflow" = $script:workflowID Write-Host " -"         $contentItem.FullPath

}

That’s it!

Bruno Nunes

Sitecore developer since 2015, passed by 7.5, 8.2, 9.1, next step will be the 10!
Self-motivated to bring new capabilities to Sitecore and to adapt it for every business case.
Currently working on Noesis as Sitecore Architect / Tech Lead.

Add comment

Bruno Nunes

Sitecore developer since 2015, passed by 7.5, 8.2, 9.1, next step will be the 10!
Self-motivated to bring new capabilities to Sitecore and to adapt it for every business case.
Currently working on Noesis as Sitecore Architect / Tech Lead.