Workflows
List of examples:
- Workflows
- Workflow steps
- Workflow scopes
- Advanced workflow
- Converting a basic workflow to advanced
- Creating advanced workflow actions
- Updating advanced workflow actions
- Updating multiple advanced workflow actions
- Deleting advanced workflow actions
- Adding steps to an advanced workflow
- Creating transitions between advanced workflow steps
- Deleting transitions between advanced workflow steps
Workflows
Creating a new workflow
// Creates a new workflow object
WorkflowInfo newWorkflow = new WorkflowInfo();
// Sets the workflow properties
newWorkflow.WorkflowDisplayName = "New workflow";
newWorkflow.WorkflowName = "NewWorkflow";
// Saves the workflow to the database
WorkflowInfo.Provider.Set(newWorkflow);
// Creates the three default workflow steps
WorkflowStepInfoProvider.CreateDefaultWorkflowSteps(newWorkflow);
Updating a workflow
// Gets the workflow
WorkflowInfo updateWorkflow = WorkflowInfoProvider.GetWorkflowInfo("NewWorkflow");
if (updateWorkflow != null)
{
// Updates the workflow properties
updateWorkflow.WorkflowDisplayName = updateWorkflow.WorkflowDisplayName.ToLowerCSafe();
// Saves the modified workflow to the database
WorkflowInfo.Provider.Set(updateWorkflow);
}
Updating multiple workflows
// Gets all workflows whose code name starts with 'New'
var workflows = WorkflowInfo.Provider.Get().WhereStartsWith("WorkflowName", "New");
// Loops through individual workflows
foreach (WorkflowInfo modifyWorkflow in workflows)
{
// Updates the workflow properties
modifyWorkflow.WorkflowDisplayName = modifyWorkflow.WorkflowDisplayName.ToUpper();
// Saves the modified workflow to the database
WorkflowInfo.Provider.Set(modifyWorkflow);
}
Deleting a workflow
// Gets the workflow
WorkflowInfo deleteWorkflow = WorkflowInfoProvider.GetWorkflowInfo("NewWorkflow");
if (deleteWorkflow != null)
{
// Deletes the workflow
WorkflowInfo.Provider.Delete(deleteWorkflow);
}
Workflow steps
Adding steps to a workflow
// Gets the workflow
WorkflowInfo workflow = WorkflowInfoProvider.GetWorkflowInfo("NewWorkflow");
if (workflow != null)
{
// Creates a new custom workflow step object
WorkflowStepInfo newStep = new WorkflowStepInfo();
// Sets the workflow step properties
newStep.StepWorkflowID = workflow.WorkflowID;
newStep.StepName = "NewWorkflowStep";
newStep.StepDisplayName = "New workflow step";
newStep.StepOrder = 1;
newStep.StepType = WorkflowStepTypeEnum.Standard;
// Saves the step to the database
WorkflowStepInfo.Provider.Set(newStep);
// Ensures correct step order for the workflow
WorkflowStepInfoProvider.InitStepOrders(workflow);
}
Allowing roles to make workflow step transitions
// Gets the workflow
WorkflowInfo workflow = WorkflowInfoProvider.GetWorkflowInfo("NewWorkflow");
if (workflow != null)
{
// Gets the custom workflow step
WorkflowStepInfo step = WorkflowStepInfoProvider.GetWorkflowStepInfo("NewWorkflowStep", workflow.WorkflowID);
if (step != null)
{
// Gets the role
RoleInfo role = RoleInfo.Provider.Get("Editor", SiteContext.CurrentSiteID);
if (role != null)
{
// Configures the workflow step to be managed only by assigned roles
step.StepRolesSecurity = WorkflowStepSecurityEnum.OnlyAssigned;
// Assigns the role to the workflow step
WorkflowStepRoleInfo.Provider.Add(step.StepID, role.RoleID);
}
}
}
Removing roles from workflow steps
// Gets the workflow
WorkflowInfo workflow = WorkflowInfoProvider.GetWorkflowInfo("NewWorkflow");
if (workflow != null)
{
// Gets the custom workflow step
WorkflowStepInfo step = WorkflowStepInfoProvider.GetWorkflowStepInfo("NewWorkflowStep", workflow.WorkflowID);
if (step != null)
{
// Gets the role
RoleInfo role = RoleInfo.Provider.Get("Editor", SiteContext.CurrentSiteID);
if (role != null)
{
// Gets relationship between the role and the workflow step
WorkflowStepRoleInfo stepRoleInfo = WorkflowStepRoleInfo.Provider.Get(step.StepID, role.RoleID);
if (stepRoleInfo != null)
{
// Removes the role from the workflow step
WorkflowStepRoleInfo.Provider.Remove(step.StepID, role.RoleID);
}
}
}
}
Deleting a workflow step
// Gets the workflow
WorkflowInfo workflow = WorkflowInfoProvider.GetWorkflowInfo("NewWorkflow");
if (workflow != null)
{
// Gets the custom workflow step
WorkflowStepInfo deleteStep = WorkflowStepInfoProvider.GetWorkflowStepInfo("NewWorkflowStep", workflow.WorkflowID);
if (deleteStep != null)
{
// Removes the step from the workflow
WorkflowStepInfo.Provider.Delete(deleteStep);
}
}
Workflow scopes
Creating a scope for a workflow
// Gets the workflow
WorkflowInfo workflow = WorkflowInfoProvider.GetWorkflowInfo("NewWorkflow");
if (workflow != null)
{
// Creates a new workflow scope object
WorkflowScopeInfo newScope = new WorkflowScopeInfo();
// Reads the code of the current site's default culture from the settings
string cultureCode = SettingsKeyInfoProvider.GetValue(SiteContext.CurrentSiteName + ".CMSDefaultCultureCode");
// Gets the culture object based on the culture code
CultureInfo culture = CultureInfo.Provider.Get(cultureCode);
// Gets the class ID of the website root page type
int classID = DataClassInfoProvider.GetDataClassInfo(SystemDocumentTypes.Root).ClassID;
// Sets the properties of the workflow scope
newScope.ScopeStartingPath = "/";
newScope.ScopeCultureID = culture.CultureID;
newScope.ScopeClassID = classID;
newScope.ScopeExcluded = false;
// Assigns the scope to the workflow for the current site
newScope.ScopeWorkflowID = workflow.WorkflowID;
newScope.ScopeSiteID = SiteContext.CurrentSiteID;
// Saves the workflow scope to the database
WorkflowScopeInfo.Provider.Set(newScope);
}
Updating workflow scopes
// Gets the workflow
WorkflowInfo workflow = WorkflowInfoProvider.GetWorkflowInfo("NewWorkflow");
if (workflow != null)
{
// Gets all scopes assigned to the specified workflow
var scopes = WorkflowScopeInfo.Provider.Get().WhereEquals("ScopeWorkflowID", workflow.WorkflowID);
// Loops through the workflow's scopes
foreach (WorkflowScopeInfo updateScope in scopes)
{
// Updates the scope to include all cultures and page types
updateScope.ScopeCultureID = 0;
updateScope.ScopeClassID = 0;
// Saves the updated workflow scope to the database
WorkflowScopeInfo.Provider.Set(updateScope);
}
}
Deleting workflow scopes
// Gets the workflow
WorkflowInfo workflow = WorkflowInfoProvider.GetWorkflowInfo("NewWorkflow");
if (workflow != null)
{
// Gets all scopes assigned to the specified workflow
var scopes = WorkflowScopeInfo.Provider.Get().WhereEquals("ScopeWorkflowID", workflow.WorkflowID);
// Loops through the workflow's scopes
foreach (WorkflowScopeInfo deleteScope in scopes)
{
// Deletes the workflow scope
WorkflowScopeInfo.Provider.Delete(deleteScope);
}
}
Advanced workflow
Converting a basic workflow to advanced
// Gets the workflow
WorkflowInfo convertWorkflow = WorkflowInfoProvider.GetWorkflowInfo("NewWorkflow");
if (convertWorkflow != null)
{
// Converts the workflow to an advanced workflow
WorkflowInfoProvider.ConvertToAdvancedWorkflow(convertWorkflow.WorkflowID);
}
Creating advanced workflow actions
// Creates a new advanced workflow action
WorkflowActionInfo newAction = new WorkflowActionInfo();
// Sets the action properties
newAction.ActionDisplayName = "New action";
newAction.ActionName = "NewAction";
newAction.ActionAssemblyName = "NewActionAssembly";
newAction.ActionClass = "Namespace.NewActionClass";
newAction.ActionEnabled = true;
newAction.ActionWorkflowType = WorkflowTypeEnum.Approval;
// Saves the advanced workflow action to the database
WorkflowActionInfo.Provider.Set(newAction);
Updating advanced workflow actions
// Gets the advanced workflow action
WorkflowActionInfo updateAction = WorkflowActionInfoProvider.GetWorkflowActionInfo("NewAction", WorkflowTypeEnum.Approval);
if (updateAction != null)
{
// Updates the workflow action properties
updateAction.ActionDisplayName = updateAction.ActionDisplayName.ToLowerCSafe();
// Saves the updated workflow action to the database
WorkflowActionInfo.Provider.Set(updateAction);
}
Updating multiple advanced workflow actions
// Gets all advanced workflow actions whose name starts with 'New'
var actions = WorkflowActionInfo.Provider.Get().WhereStartsWith("ActionName", "New");
// Loops through individual workflow actions
foreach (WorkflowActionInfo modifyAction in actions)
{
// Updates the workflow actions properties
modifyAction.ActionDisplayName = modifyAction.ActionDisplayName.ToUpper();
// Saves the updated workflow action to the database
WorkflowActionInfo.Provider.Set(modifyAction);
}
Deleting advanced workflow actions
// Gets the advanced workflow action
WorkflowActionInfo deleteAction = WorkflowActionInfoProvider.GetWorkflowActionInfo("NewAction", WorkflowTypeEnum.Approval);
if (deleteAction != null)
{
// Deletes the workflow action
WorkflowActionInfo.Provider.Delete(deleteAction);
}
Adding steps to an advanced workflow
// Gets the advanced workflow
WorkflowInfo workflow = WorkflowInfoProvider.GetWorkflowInfo("NewWorkflow");
if (workflow != null)
{
// Creates a new step for the workflow
WorkflowStepInfo newStep = new WorkflowStepInfo()
{
StepWorkflowID = workflow.WorkflowID,
StepName = "NewStep",
StepDisplayName = "New step",
StepType = WorkflowStepTypeEnum.Standard
};
// Saves the workflow step to the database
WorkflowStepInfo.Provider.Set(newStep);
}
Creating transitions between advanced workflow steps
// Gets the advanced workflow
WorkflowInfo workflow = WorkflowInfoProvider.GetWorkflowInfo("NewWorkflow");
if (workflow != null)
{
// Gets the workflow's 'NewStep' and 'Published' steps
WorkflowStepInfo newStep = WorkflowStepInfoProvider.GetWorkflowStepInfo("NewStep", workflow.WorkflowID);
WorkflowStepInfo publishedStep = WorkflowStepInfoProvider.GetWorkflowStepInfo("Published", workflow.WorkflowID);
if ((newStep != null) && (publishedStep != null))
{
// Gets the workflow's existing transition leading to the 'Published' step
WorkflowTransitionInfo existingTransition = WorkflowTransitionInfo.Provider.Get()
.WhereEquals("StepWorkflowID", workflow.WorkflowID)
.WhereEquals("TransitionEndStepID", publishedStep.StepID)
.TopN(1)
.FirstOrDefault();
// Modifies the existing transition to lead to the 'NewStep' step instead of the 'Published' step
existingTransition.TransitionEndStepID = newStep.StepID;
// Saves the updated transition to the database
WorkflowTransitionInfo.Provider.Set(existingTransition);
// Creates a new transition from the 'NewStep' step to the 'Published' step
newStep.ConnectTo(newStep.StepDefinition.SourcePoints[0].Guid, publishedStep);
}
}
Deleting transitions between advanced workflow steps
// Gets the advanced workflow
WorkflowInfo workflow = WorkflowInfoProvider.GetWorkflowInfo("NewWorkflow");
if (workflow != null)
{
// Gets the workflow's 'NewStep' step
WorkflowStepInfo startStep = WorkflowStepInfoProvider.GetWorkflowStepInfo("NewStep", workflow.WorkflowID);
if (startStep != null)
{
// Gets all transitions leading from the workflow's 'NewStep' step
var existingTransitions = WorkflowTransitionInfo.Provider.Get()
.WhereEquals("StepWorkflowID", workflow.WorkflowID)
.WhereEquals("TransitionStartStepID", startStep.StepID);
// Loops through the step transitions
foreach (WorkflowTransitionInfo deleteTransition in existingTransitions)
{
// Deletes the step transition
WorkflowTransitionInfo.Provider.Delete(deleteTransition);
}
}
}