4

How can we stop Un-Publish in event system ?

My Requirement is to stop unpublishing in Event System and put the item into bundle X and start a WorkFlow Y on this bundle .

I am able to achieve adding item into bundle and able to trigger workflow also on this. What is happening is my item got unpublished all the time and Workflow Y also trigger all the time.

I want to stop the unpublihsing and want to start WorkFlow.

I have tried this till now

        Bundle bundle = (Bundle)item.Session.GetObject("tcm:26-172-8192");
        bundle.AddItem(item);          
        bundle.Save();         

        StartWorkflowInstruction instruction = new StartWorkflowInstruction(item.Session);        
        instruction.Subjects.Add(bundle);         
        ProcessInstance result = bundle.ContextRepository.StartWorkflow(instruction);

SQL Error on mPublishTransaction.Delete();

StackTrace Information Details:
   at Tridion.ContentManager.Data.AdoNet.Sql.SqlDatabaseUtilities.Execute(StoredProcedureInvocation storedProcedureInvocation)
   at Tridion.ContentManager.Data.AdoNet.IdentifiableObjectDataMapper.Read(TcmUri id, StoredProcedureInvocation spInvocation)
   at Tridion.ContentManager.Data.AdoNet.IdentifiableObjectDataMapper.Tridion.ContentManager.Data.IIdentifiableObjectDataMapper.Read(TcmUri id)
   at Tridion.ContentManager.Session.GetDataObject(TcmUri id)
   at Tridion.ContentManager.IdentifiableObject.Load(LoadFlags flags, Boolean forceReload)
   at Tridion.ContentManager.IdentifiableObject.Reload()
   at Tridion.ContentManager.IdentifiableObject.get_Description()
   at MetLife.ADD.Tridion.Eventing.PageUnPublishWorkFlow.AddItemToUnPublishBundle(RepositoryLocalObject item, UnPublishEventArgs args)
   at MetLife.ADD.Tridion.Eventing.PageUnPublishWorkFlow.PageUnPublishWorkFlowChange(Page subject, UnPublishEventArgs args, EventPhases phase)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at Tridion.ContentManager.Extensibility.EventSubscription.<DeliverEvent>b__0(AsynchronousEventWorkItem workItem)
   at Tridion.ContentManager.StaThreadPool`1.StaThread.DoWork()
  • 1
    Please elaborate on what you're trying to achieve, what you've tried so far and why it isn't working. – Ant P Feb 26 '14 at 10:04
  • If the sample code you provided is executed in your unpublish event handler, then indeed that will not stop the unpublish action. To stop an action from happening in an event handler, you will have to raise an exception (you cannot catch the exception, since then the action will continue after your event handler finished). It might indeed be a better solution for you to remove the unpublish action via a resolver. – Bart Koopman Feb 26 '14 at 10:34
  • Can i put resolver to stop Publish with in Event System ?As I need to Trigger WorkFlow on this item through Bundle. – Jitender sharma Feb 26 '14 at 10:50
  • A Resolver is something different than an Event Handler, but you use the TOM.NET API in both. If you create a Resolver, then your Event Handler will never be executed, since the Resolver removes the item from the Publish Transaction. Or if you can live with the exception being raised (give it a decent message), you can solve it in an Event Handler. – Bart Koopman Feb 26 '14 at 13:51
3

You can use custom resolver to stop publishing/un-publishing of the items you want .

In custom resolver you can implement your business logic what to publish or not.

basically, In custom resolver you can manage the resolved items against a published item.

you can find the more info here (Login Required)

| improve this answer | |
  • I need to Stop Publishing and Start Workflow on the Bundle.I am not sure i can do this in resolver. – Jitender sharma Feb 26 '14 at 10:59
  • Keep your existing event system in place as its doing its Job by starting workflow. just put Stop-Unpublishing-logic in Resolver. It should work. – Saurabh Gangwar Feb 26 '14 at 11:04
  • So Resolver kicks in before Event System or At the time of resolving the page for unpublish it will Stop unpublish and Event System will kick in the WorkFlow. Will it be a problem if my last step in WorkFlow if unpublish? – Jitender sharma Feb 26 '14 at 11:09
  • well, the resolver is called by publisher service while you publish or unpublish item. As I don't have any idea about your workflow. and also at which event you are starting the workflow. I suggest you to test at your end and adjust accordingly with the events fired from Tridion. I don't think resolver will interfere in raising "Un-Publish" event of tridion. – Saurabh Gangwar Feb 26 '14 at 11:19
2

As an alternative of using the Event System, you could consider the following:

Seems that whatever you want to do is overwrite the behavior of unpublish. If that is the case, instead to use the Event System maybe you could think in a UI extension.

You could just disable the option of unpublish (for the items that match certain conditions based on your analysis) and add another option (button and right click menu) named for instance "add to bundle for unpublish" that captures the item and will execute that functionality (add to the bundle, start the workflow, etc). You can popup a window where the user confirms that wants to execute that action and receives the confirmation message.

In that case the unpublish event won't be kicked off.

Note: Check if the scenario is applicable to any item that is unpublished or the items must match specific conditions

| improve this answer | |
  • 2
    +1, I think the best option is to extend the Unpublish command and stop the propagation from there, you can do it using the pipeline.stop = true; property. – Eric Huiza Feb 28 '14 at 16:13
0

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.

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);
}
| improve this answer | |
  • @Shiva : mPublishTransaction.Delete(); Does not work , It gives some SQL error which is not very informative and I also tried clearing the unpublishinstructions which is not working as well. – Jitender sharma Feb 27 '14 at 4:39
  • @Jitendersharma: Please share the SQL Error. – Siva Charan Feb 27 '14 at 4:55
  • @Shiva : I have added the SQL logs in question.Can you please move it to appropriate place if it is at correct palce.As i said Logs are not more informative. – Jitender sharma Feb 27 '14 at 7:38
  • @Jitendersharma: I have updated my answer which is tested and working fine as expected. – Siva Charan Feb 27 '14 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.