Skip to main content
added 1224 characters in body
Source Link
Siva Charan
  • 4.3k
  • 1
  • 20
  • 36

Update:

*Note that, I didn't get a chance to execute it. So the code is Un-TestedToday I got a chance to test my code. After testing the code, I realised that where I have done the mistake.

To delete the PublishTransaction, you need to Subscribe an Event in Processed Phase.

Below is modified code, which works perfectly:

public EventSystemHandler()
{
    EventSystem.Subscribe<Page, UnPublishEventArgs>(StopUnPublishing, EventPhases.Processed);
    EventSystem.Subscribe<Component, UnPublishEventArgs>(StopUnPublishing, EventPhases.Processed);
}

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

    List<PublishTransaction> mPublishTransactionList = mUnPublishEventArgs.PublishTransactions.ToList();

    foreach (PublishTransaction mPublishTransaction in mPublishTransactionList)
    {
        foreach (IdentifiableObject identifiableObject in mPublishTransaction.Items)
        {
            mListIdentifiableObject.Add(identifiableObject);
        }

        mPublishTransaction.Delete();
    }

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

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

Update:

Today I got a chance to test my code. After testing the code, I realised that where I have done the mistake.

To delete the PublishTransaction, you need to Subscribe an Event in Processed Phase.

Below is modified code, which works perfectly:

public EventSystemHandler()
{
    EventSystem.Subscribe<Page, UnPublishEventArgs>(StopUnPublishing, EventPhases.Processed);
    EventSystem.Subscribe<Component, UnPublishEventArgs>(StopUnPublishing, EventPhases.Processed);
}

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

    List<PublishTransaction> mPublishTransactionList = mUnPublishEventArgs.PublishTransactions.ToList();

    foreach (PublishTransaction mPublishTransaction in mPublishTransactionList)
    {
        foreach (IdentifiableObject identifiableObject in mPublishTransaction.Items)
        {
            mListIdentifiableObject.Add(identifiableObject);
        }

        mPublishTransaction.Delete();
    }

    // Create Bundle and Starts the workflow
    StartBundleWorkFlow("tcm:26-172-8192", mListIdentifiableObject);
}
added 78 characters in body
Source Link
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

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.

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

Source Link
Siva Charan
  • 4.3k
  • 1
  • 20
  • 36

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.