Skip to main content
2 of 3
added 78 characters in body
Siva Charan
  • 4.3k
  • 1
  • 20
  • 36

Well, it's a good question. It made me to think a lot on how to implement.

After a lot of time spend, I got an idea like

Why can't we delete the publish transaction before activity commits

I wrote a generic code for you, which works for any IdentifiableObject. Just you need to do is Subscribe for an IdentifiableObject.

Based on these, I come with the below code:

public class EventSystemHandler : TcmExtension
{
    public EventSystemHandler()
    {
        EventSystem.Subscribe<Page, UnPublishEventArgs>(StopUnPublishing, EventPhases.Initiated);
        EventSystem.Subscribe<Component, UnPublishEventArgs>(StopUnPublishing, EventPhases.Initiated);
    }

    public void StopUnPublishing(IdentifiableObject mIdentifiableObject, UnPublishEventArgs mUnPublishEventArgs, EventPhases mEventPhases)
    {
        IList<IdentifiableObject> mListIdentifiableObject = null;

        foreach (PublishTransaction mPublishTransaction in mUnPublishEventArgs.PublishTransactions.ToList())
        {
            foreach (IdentifiableObject identifiableObject in mPublishTransaction.Items)
            {
                mListIdentifiableObject.Add(identifiableObject);
            }
            mPublishTransaction.Delete();
        }

        // Create Bundle and Starts the workflow
        StartBundleWorkFlow("tcm:26-172-8192", mListIdentifiableObject);
    }

    public void StartBundle(TcmUri bundleId, IList<IdentifiableObject> identifiableObjectList)
    {
        Bundle bundle = (Bundle)mSession.GetObject(bundleId);
        foreach (IList<IdentifiableObject> identifiableObject in identifiableObjectList)
        {
            bundle.AddItem(identifiableObject);
        }
        bundle.Save();

        StartWorkflowInstruction instruction = new StartWorkflowInstruction(mSession);
        instruction.Subjects.Add(bundle);
        ProcessInstance result = bundle.ContextRepository.StartWorkflow(instruction);
        // Do Your Stuff     
    }
}

Hope this will help you.

*Note that, I didn't get a chance to execute it. So the code is Un-Tested

Siva Charan
  • 4.3k
  • 1
  • 20
  • 36