Create workflow definitions

NEW TO SITEFINITY?

When you go to the Sitefinity CMS administrative backend Administration -> Workflow menu, and define a workflow, you are configuring the properties of a workflow definition object.  You can do the same thing using the Workflow API, for example when you need to load a different workflow for different items, without this workflow being already created. To create a workflow definition, you need to specify its Title, Scope, Levels of approval, and all additional properties that determine the workflow behavior.

The following sample demonstrates creating two workflow definitions:

  • A 1-step approval workflow called “1-step workflow for Bloggers”, where users that are assigned to the Backend users role can approve content

    and

  • A 2-step approval workflow called "2-step workflow for Restricted Users", where users that are assigned to the Designers role can approve content, and users that belong to the Editorsrole can publish it
using System;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Pages.Model;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Configuration;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Workflow;
using Telerik.Sitefinity.Workflow.Model;
namespace SitefinityWebApp.CustomWorkflow
{
public class WorkflowDefinitionSamples
{
private IWorkflowExecutionDefinition CreateSimpleApprovalWorkflowDefinition(Guid definitionId)
{
var workflowManager = WorkflowManager.GetManager();
var backendUsersRole = Config.Get<SecurityConfig>().ApplicationRoles[SecurityConstants.AppRoles.BackendUsers];
// "Approve" is the only level in 1-step approval process.
var workflowDefinition = workflowManager.CreateWorkflowDefinition(definitionId);
workflowDefinition.Title = "1-step workflow for Bloggers";
workflowDefinition.WorkflowType = WorkflowType.StandardOneStep;
workflowDefinition.AllowAdministratorsToSkipWorkflow = false;
workflowDefinition.AllowPublishersToSkipWorkflow = false;
workflowDefinition.AllowNotes = true;
workflowDefinition.IsActive = true;
// Levels section
var approvePermission = workflowManager.CreateWorkflowPermission();
approvePermission.ActionName = "Approve";
approvePermission.PrincipalType = WorkflowPrincipalType.Role;
approvePermission.PrincipalId = backendUsersRole.Id.ToString();
approvePermission.PrincipalName = backendUsersRole.Name;
var level1 = workflowManager.CreateWorkflowLevel();
level1.ActionName = "Approve";
level1.Ordinal = 1;
level1.NotifyAdministrators = true;
level1.NotifyApprovers = true;
level1.Permissions.Add(approvePermission);
workflowDefinition.Levels.Add(level1);
// Scopes section - determines whether the workflow is valid for a specific site or language
var workflowScope1 = workflowManager.CreateWorkflowScope();
// You can specify whether the workflow is valid for a specified site only
// If you omit this line the workflow is valid for all sites by default
workflowManager.Provider.AddItemLink(SystemManager.CurrentContext.CurrentSite.Id, workflowScope1);
// You can specify whether the workflow is valid for a specified language only
// If you omit this line the workflow is valid for all languages by default
workflowScope1.Language = "en";
//TypeScope section - determines whether the workflow is valid for a specific item type or specific page(s) if the item type is PageNode
var typeScope1 = workflowManager.CreateWorkflowTypeScope();
//Specify this workflow to be valid for Newsitems
typeScope1.ContentType = typeof(NewsItem).FullName;
var typeScope2 = workflowManager.CreateWorkflowTypeScope();
//specify this workflow to be valid for Pages
typeScope2.ContentType = typeof(PageNode).FullName;
// If you want to specify only selected pages, pass a comma separated list of PageNode Ids to the ContentFilter
typeScope2.ContentFilter = "";
//Ste the IncludeChildren property to true/false to determine whether to include child pages of the selected page(s)
typeScope2.IncludeChildren = true;
workflowScope1.TypeScopes.Add(typeScope1);
workflowScope1.TypeScopes.Add(typeScope2);
workflowDefinition.Scopes.Add(workflowScope1);
workflowManager.SaveChanges();
return new WorkflowDefinitionProxy(workflowDefinition);
}
private IWorkflowExecutionDefinition CreateTwoStepApprovalWorkflowDefinition(Guid definitionId)
{
var workflowManager = WorkflowManager.GetManager();
var backendUsersRole = Config.Get<SecurityConfig>().ApplicationRoles[SecurityConstants.AppRoles.BackendUsers];
// In 2-level approval the steps are "Approve" and "Publish".
// In 3-level approval the steps are "Review", "Approve" and "Publish".
var workflowDefinition = workflowManager.CreateWorkflowDefinition(definitionId);
workflowDefinition.Title = "2-step workflow for Restricted Users";
workflowDefinition.WorkflowType = WorkflowType.StandardTwoStep;
workflowDefinition.AllowAdministratorsToSkipWorkflow = false;
workflowDefinition.AllowPublishersToSkipWorkflow = false;
workflowDefinition.AllowNotes = true;
workflowDefinition.IsActive = true;
// Levels section
// Level 1 - Approve
// Allow everyone in the role "Designers" to approve content.
var approver1 = Config.Get<SecurityConfig>().ApplicationRoles[SecurityConstants.AppRoles.Designers];
var approvePermission = workflowManager.CreateWorkflowPermission();
approvePermission.ActionName = "Approve";
approvePermission.PrincipalType = WorkflowPrincipalType.Role;
approvePermission.PrincipalId = approver1.Id.ToString();
approvePermission.PrincipalName = approver1.Name;
var level1 = workflowManager.CreateWorkflowLevel();
level1.ActionName = "Approve";
level1.Ordinal = 1;
level1.NotifyAdministrators = true;
level1.NotifyApprovers = true;
level1.Permissions.Add(approvePermission);
workflowDefinition.Levels.Add(level1);
//Level 2 - Publish
var level2 = workflowManager.CreateWorkflowLevel();
level2.ActionName = "Publish";
level2.Ordinal = 2;
level2.NotifyAdministrators = true;
level2.NotifyApprovers = true;
// Allow everyone in the role "Editors" to publish content.
var publisher1 = Config.Get<SecurityConfig>().ApplicationRoles[SecurityConstants.AppRoles.Editors];
var publishPermission1 = workflowManager.CreateWorkflowPermission();
publishPermission1.ActionName = "Publish";
publishPermission1.PrincipalType = WorkflowPrincipalType.Role;
publishPermission1.PrincipalId = publisher1.Id.ToString();
publishPermission1.PrincipalName = publisher1.Name;
level2.Permissions.Add(publishPermission1);
// Allow user "johnsmith" (if exists) to publish content.
var publisher2 = UserManager.FindUser("johnsmith");
// In 2-level approval the second step is "Publish"
if (publisher2 != null)
{
var publishPermission2 = workflowManager.CreateWorkflowPermission();
publishPermission2.ActionName = "Publish";
publishPermission2.PrincipalType = WorkflowPrincipalType.User;
publishPermission2.PrincipalId = publisher2.Id.ToString();
publishPermission2.PrincipalName = publisher2.UserName.ToString();
level2.Permissions.Add(publishPermission2);
}
workflowDefinition.Levels.Add(level1);
// Scopes section
var workflowScope1 = workflowManager.CreateWorkflowScope();
workflowDefinition.Scopes.Add(workflowScope1);
workflowManager.SaveChanges();
return new WorkflowDefinitionProxy(workflowDefinition);
}
}
}

Want to learn more?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?