Skip to content

Instantly share code, notes, and snippets.

@DavidForster
Created October 2, 2014 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidForster/a10745210290c5c61fba to your computer and use it in GitHub Desktop.
Save DavidForster/a10745210290c5c61fba to your computer and use it in GitHub Desktop.
SDL Tridion 2013SP1 Event System to trigger an "Instant Approval" workflow on components that become unapproved after editing. See https://tridion.stackexchange.com/questions/8054/old-workflow-items-set-to-unapproved
using System;
using System.Linq;
using System.Xml;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.Extensibility.Events;
using Tridion.ContentManager.Workflow;
namespace ApproveOutOfWorkflowComponents
{
static internal class Events
{
private const string ProcessDefinitionName = "Instant Approval";
internal static void AfterComponentCheckIn(Component component, CheckInEventArgs args, EventPhases phase)
{
var session = component.Session;
//Don't continue unless...
if (args.CheckInAction != CheckInAction.CheckIn ||
!component.IsUnapproved() ||
component.Schema.HasWorkflowAssociation())
return;
//Start a simple automated workflow process which will Approve the component
var instruction = new StartWorkflowInstruction(session);
instruction.Subjects.Add(component);
instruction.ProcessInstanceTitle = String.Format("Automatic approval for component {0} ({1})", component.Title, component.Id);
//instruction.Assignee = component.Revisor;
//Find the workflow process definition
var filter = new ProcessDefinitionsFilter(session) { ContextRepository = component.ContextRepository };
var definitionList = session.GetList(filter).Cast<XmlElement>();
var definitionElement = definitionList.First(element => element.Attributes["Title"].Value == ProcessDefinitionName);
if (definitionElement == null)
return;
instruction.ProcessDefinition = (ProcessDefinition)session.GetObject(definitionElement.Attributes["ID"].Value);
component.ContextRepository.StartWorkflow(instruction);
}
}
}
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.Extensibility.Events;
namespace ApproveOutOfWorkflowComponents
{
[TcmExtension("Automatically approve out of workflow components")]
public class EventSubscription : TcmExtension
{
public EventSubscription()
{
EventSystem.Subscribe<Component, CheckInEventArgs>(Events.AfterComponentCheckIn, EventPhases.TransactionCommitted);
}
}
}
using Tridion.ContentManager.ContentManagement;
namespace ApproveOutOfWorkflowComponents
{
internal static class ExtensionMethods
{
private const string UnapprovedApprovalStatusId = "tcm:0-0-131073";
public static bool IsUnapproved(this Component component)
{
return (component.ApprovalStatus != null && component.ApprovalStatus.Id == UnapprovedApprovalStatusId);
}
public static bool HasWorkflowAssociation(this Schema schema)
{
return (schema.ComponentProcess != null &&
!schema.ComponentProcess.Id.IsUriNull) ||
(schema.BundleProcess != null &&
!schema.BundleProcess.Id.IsUriNull);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment