How to determine in a Custom Resolver if I am publishing or unpublishing? Tridion 2009 SP1

StackOverflow https://stackoverflow.com/questions/15153245

  •  16-03-2022
  •  | 
  •  

Question

I'm trying to capture when a component is unpublished. I try some approaches but I don't have the result that I want. My attempts are:

  1. In Event System. But this not works because there are a known bug in Windows about MSXML and COM+.

  2. I try to build my own IResolver but there I cannot determine if it's a publishing or unpublishing action.

  3. I try to build my own ITransportPackageHandler. There, I have a function called HandleResolvedItemForUnPublishing but I don't have any information about PublicationTarget and I don't know if it's unpublished from staging or live.

Can someone help me? I think that I can solve the problem if:

  1. At the IResolver I can determine if the component is unpublishing.
  2. At the ITransportPackageHandler I can access to the PublicationTarget info
  3. If I can pass info from IResolver to the ITransportPackageHandler in a context variable or something similar.

thank you very much.

Gustavo.

Was it helpful?

Solution

You should be able to look at the ResolvePurpose of the ResolveInstruction that you get as one of the parameters in the custom resolver. Something along these lines:

public void Resolve(IdentifiableObject item, ResolveInstruction instruction, PublishContext context, ISet<ResolvedItem> resolvedItems)
{
    if (instruction.Purpose == ResolvePurpose.Publish || instruction.Purpose == ResolvePurpose.RePublish)
    { 
        // We are publishing
    }
    else if(instruction.Purpose == ResolvePurpose.UnPublish)
    {
        // We are unpublishing
    }
    // Don't know if this one exists in 2009, it exists in 2011 SP1
    else if(instruction.Purpose == ResolvePurpose.UnknownByClient)
    {
        // The server is doing something that I don't understand (yet?)
    }
}

EDIT

I refused to not find a way to make this work...

Indeed, in Tridion 2009 you don't have a Purpose on the resolve instruction. You do have an Action in the Publish Transaction, but this one is not exposed directly in the resolver. Here's how I found out if I'm publishing or unpublishing - your call if think it's overkill, but performance on my non-production VM was pretty good.

  1. Find the current item we're resolving for
  2. Load the list of PublishTransaction with a state of "In Progress"
  3. Find the transaction for the current item
  4. Determine the action by looking at the Action attribute
Filter filter = new Filter();
filter.Conditions["InfoType"] = 2; // Get transactions in Progress
foreach (XmlNode node in item.Session.GetList(typeof(PublishTransaction), filter))
{
    if(node.Attributes["ItemID"].Value.Equals(item.Id.ToString()))
    {
        // we have a winner
        string action;
        if (node.Attributes["Action"].Value.Equals("0"))
            action = "Publish";
        if (node.Attributes["Action"].Value.Equals("1"))
            action = "Unpublish";
    }
}

OTHER TIPS

Assuming you use 2011 you can bind an event handler to the Publish Transaction Save event and verify the State. Then, when the Component is unpublished you can perform the logic you needed.

public sealed class PublishedToEventHandler: TcmExtension
{
    public PublishedToEventHandler()
    {
        EventSystem.SubscribeAsync<PublishTransaction, SaveEventArgs>(
            (subject, args, phase) =>
            {
                if (!PublishStransactionStateIsSuccessfullyCompleted(subject))
                    return;



            },
            EventPhases.TransactionCommitted
        );
    }

    static bool PublishStransactionStateIsSuccessfullyCompleted(PublishTransaction transaction)
    {
        return transaction.State == PublishTransactionState.Success ||
               transaction.State == PublishTransactionState.Warning;
    }
}

Before anything is handled in this event you can verify the Instruction.ResolveInstruction.Purpose property of the transaction to see whether or not you are publishing on unpublishing.

The transaction has a ProcessedItems collection, and each contains the Page or Component in the ResolvedItem.Item propery of the ProcessedItem object. When its a Page you do need to obtain the Components embedded on the Page to do anything with them.

Let me know if you have any more questions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top