All About Customizing Workflow in Sitecore
All About Customizing Workflow in Sitecore
workflow in Sitecore
DEVELOPERS REFERENCE GUIDE
By Surendra Sharma
TECHNICAL ARCHITECT | HTTP://SURENDRASHARMADOTNET.BLOGSPOT.IN/
Lets deep dive into understanding Sitecore workflow with one real life requirement and its
implementation.
Requirement:
Client will need the following roles:
Editor Can edit content but cannot publish content.
Reviewer Can edit and publish content
An editor needs to be able to assign content to an individual reviewer to review and
publish.
Below is a typical scenario:
Editor A creates/updates to an item for example product item. Editor A saves the
item content then assigns it to Reviewer A to review and publish.
1|Page
The scenario above will be the most common scenario for reviewing and publishing
content.
For a multilingual website, client need language groups with scenario:
Editor X in Germany makes an update to a German-language product. Editor X saves
the content but isn't sure what individual reviewer to assign it to. Editor X thus
assigns it to the German Review Group to review and publish. The German Review
Group includes several members of the German marketing team (Reviewer X,
Reviewer Y and Reviewer Z).
2|Page
Reviewer X, Reviewer Y and Reviewer Z each get an email that a piece of content is
available for review. Reviewer Z agrees to review the content and publish it.
3|Page
Solution:
We will implement this requirement with below steps
1. Create Editor and Reviewer Roles
2. Assign permissions on items to roles
3. Create editor and reviewer users
4. Map users with roles
5. Create individual/group reviewer items in content tree
6. Create Custom comment window
7. Apply validation rules on comment windows field
8. Create Custom workflow
9. Apply custom workflow on items
10. Customizing workbox
11. Customizing items comments in Awaiting Approval section
12. Test all the scenarios
4|Page
5|Page
6|Page
Similarly assign permissions to Reviewer Group Role. Note that we are providing
Delete Descendants as well as Administer level permission to this role.
Tips: - If you are not assigning item access permission to Roles/Users, then that
user will not able to see workflow pending items in workbox.
7|Page
Create some new users who play part of editors and reviewers.
For example, we created 4 users like
Editor1
Editor2
Reviewer1
Reviewer2
8|Page
9|Page
5. Create custom multilist for showing all user who are reviewers and admin
We need to show all admin and reviewer into multilist. However Sitecore dont have
any way to show all these user (more specifically all Reviewer). So we have to create
custom multilist control and fill all the reviewers and admin.
Steps to create custom multilist
Select CORE database in Sitecore desktop mode
Add new item as Custom Reviewer in /sitecore/system/Field types/List
Types/ as below
10 | P a g e
Add a class in your UI solution and copy-paste below code for creating custom
multilist which get Reviewer Group role and domain name from config file and
get all users under reviewer role and admin
using
using
using
using
using
using
using
using
using
using
using
using
using
System;
System.Collections;
System.Collections.Generic;
System.Linq;
Sitecore.Text;
Sitecore.Security.Accounts;
System.Web.Security;
Sitecore.Configuration;
Sitecore.Diagnostics;
System.Web.UI;
Sitecore.Resources;
Sitecore.Globalization;
Sitecore.Shell.Applications.ContentEditor;
namespace SitecoreWebApp.UI
{
public interface IUsersField
{
IEnumerable<User> GetSelectedUsers();
IEnumerable<User> GetUnselectedUsers();
string GetProviderUserKey(User user);
}
/// <summary>
/// Class to get admin and user from reviewer group
/// </summary>
public class UsersField : IUsersField
{
private static readonly string DomainParameterName =
Settings.GetSetting("UsersField.DomainParameterName");
private static readonly string RoleParameterName =
Settings.GetSetting("UsersField.RoleParameterName");
private ListString _SelectedUsers;
private ListString SelectedUsers
{
get
{
if (_SelectedUsers == null)
{
_SelectedUsers = new ListString(Value);
}
return _SelectedUsers;
}
}
11 | P a g e
12 | P a g e
13 | P a g e
14 | P a g e
15 | P a g e
16 | P a g e
17 | P a g e
18 | P a g e
Create Reviewer Templates with two fields Name and Reviewer. Note
Reviewer field is of Custom Reviewer type.
Now we have to create individual and group reviewers items in content tree.
For this, create folder Content Reviewers in content tree and restrict insert
option for Reviewer template items only
Note we are getting all user who are reviewer and admin in our custom multi list
for this newly added item.
For creating Reviewer Group, select more than one reviewer from multilist as
19 | P a g e
20 | P a g e
As shown in above image, we have standard Comments multiline textbox and newly
added field Preferred Reviewer which is of droplist type which pointed to all
reviewer from content tree folder Content Reviewers.
21 | P a g e
22 | P a g e
Note instead of our new template in Default Comment Template field, we are
still using Standard Comment Template as new template should only use when editor
submit item to Reviewer.
Draft This is first stage of all workflow. This state remain blank as below
23 | P a g e
System.Linq;
Sitecore.Workflows;
System.Web.Security;
Sitecore.Data;
Sitecore.Data.Fields;
namespace SitecoreWebApp.UI
{
public class CommandAppearanceEvaluator : BasicWorkflowCommandAppearanceEvaluator
{
public override WorkflowCommandState EvaluateState(Sitecore.Data.Items.Item item,
Sitecore.Data.Items.Item workflowCommand)
{
if (Sitecore.Context.User.IsAdministrator)
{
return WorkflowCommandState.Visible;
}
if (item != null) //the item might be null!
24 | P a g e
25 | P a g e
Email To Reviewer
We want to send mail to Reviewer with message There is an item for your approval
in your workbox & the item path is $itemPath$ in language $itemLanguage$ and
version $itemVersion$.
Where $itemPath$, $itemLanguage$ and $itemVersion$ should be replaced by their
value in code specified in Type field as
26 | P a g e
System;
System.Collections;
System.Collections.Generic;
System.Linq;
Sitecore.Data.Items;
Sitecore.Security.Accounts;
System.Web.Security;
Sitecore.Diagnostics;
Sitecore.Data;
Sitecore.Data.Fields;
Sitecore.Workflows.Simple;
System.Net.Mail;
System.Net;
Sitecore.Configuration;
namespace SitecoreWebApp.UI
{
public class EmailAction
{
// Methods
private string GetText(Item commandItem, string field, WorkflowPipelineArgs args)
{
string text = commandItem[field];
if (text.Length > 0)
{
return this.ReplaceVariables(text, args);
}
return string.Empty;
}
public void Process(WorkflowPipelineArgs args)
{
Assert.ArgumentNotNull(args, "args");
27 | P a g e
//Network Credentials
string host = Settings.GetSetting("MailServer");
var smtpClient = new SmtpClient(host);
smtpClient.Credentials = new
NetworkCredential(Settings.GetSetting("MailServerUserName"),
Settings.GetSetting("MailServerPassword"));
int port;
smtpClient.Port =
int.TryParse(Settings.GetSetting("MailServerPort"), out port) ? port : 25;
string mailSubject = this.GetText(innerItem, "subject", args);
string mailMessage = this.GetText(innerItem, "message", args);
Error.Assert(to.Length > 0, "The 'To' field is not specified in
the mail action item: " + fullPath);
Error.Assert(from.Length > 0, "The 'From' field is not specified
in the mail action item: " + fullPath);
Error.Assert(mailSubject.Length > 0, "The 'Subject' field is not
specified in the mail action item: " + fullPath);
Error.Assert(host.Length > 0, "The 'Mail server' field is not
specified in the mail action item: " + fullPath);
foreach (string tempTo in to.Split(new char[] { ';' }))
28 | P a g e
29 | P a g e
30 | P a g e
Awaiting Approval
This state remain blank like draft state.
Approve
Reviewer takes the action on item and approving for Publish.
In Appearance Evaluator Type field, I want to customize the same action as
mentioned in Submit Command code.
Email To Editor
Send approval mail message to editor. Functionality and setting is same as described
in Email To Reviewer section.
Reject
Reviewer takes the action on item and rejecting the item for some
correction/improvements. This step again send item for draft stage.
In Appearance Evaluator Type field, I want to customize the same action as
mentioned in Submit Command code.
31 | P a g e
32 | P a g e
33 | P a g e
Editor can also submit item to group reviewer. All reviewer within a group should
receive a review request mail.
34 | P a g e
If editor will not select any reviewer, a warning message should display. Editor can
still submit the item which finally review only by reviewer who is admin.
35 | P a g e
36 | P a g e
37 | P a g e
38 | P a g e
39 | P a g e
Rest of the item should be iterate by following recursive function and apply workflow on all
the items except folder item.
private Item ApplyWorkflow(Item mainItem, int icounter)
{
try
{
if (!mainItem.DisplayName.Equals("Homepage"))
{
//Put Workflow ID here
var workflow =
Factory.GetDatabase("master").WorkflowProvider.GetWorkflow("{7E9BC450-18EE-401E-892A1CEF27BF8D9B}");
workflow.Start(mainItem);
}
}
catch
{
}
if (mainItem.HasChildren)
{
icounter++;
foreach (var myInnerItem in mainItem.Children.ToList())
{
ApplyWorkflow(myInnerItem, icounter);
}
}
icounter--;
return null;
}
40 | P a g e
Scenario 2: Editor edits any item and submits for approval without any reviewer selection
User Action:
Editor 1 Item 2 version 1 submits - no one
Expected Outcome:
1. Only users having Admin access will be able to approve this version of item and no
one else.
2. Email notification triggered to the editor once item is approved and that approved
version of item is published and live
41 | P a g e
Scenario 4: Item submitted to approver Group and multiple approver in Group try to
open/approve that item
User Action:
Reviewer G1 - Item 3 - opened by multiple reviewer of G1.
G1 R1 - Opens, edit then approve
G1 R2 - Opens, approve
Expected Outcome:
1. Workbox of those reviewer (group) will show the link to approve those versions of
item to whom it is assigned to.
2. Email notification triggered to the reviewer/ Reviewer group on item submit
3. Email notification triggered to the editor once item is approved and that approved
version of item is published and live
4. The Approval link/ button can be submitted once by the reviewer who clicked that
first. On refresh the workbox of the other user will not display the item event if he is
about to submit that.
42 | P a g e
Scenario 6: Item submitted to approver Group and multiple approver in Group try to do
different action for that item
User Action:
Item 3 version 2 submitted for approval by Editor 2 to Reviewer G 1
G1 R1 - Opens, edit then approve
G1 R2 - Rejects
Expected Outcome:
1. Workbox of those reviewer (group) will show the link to approve those versions of
item to whom it is assigned to.
2. Email notification triggered to the reviewer/ Reviewer group on item submit.
3. Approve or Rejection by the approver depends on who clicks the link first and other
approvers browsers will be refreshed on page refresh.
Email notification triggered to the editor once item is approved/Rejected and that approved
version of item is published and live.
43 | P a g e
References
http://blog.rauljimenez.co.uk/a-comment-on-comments/
https://sitecorejunkie.com/2012/12/28/have-a-field-day-with-custom-sitecore-fields/
http://stackoverflow.com/questions/18754931/sitecore-workbox-approve-all-configure-security
44 | P a g e