Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the (un-) PublicationTarget for component UnPublish event in Tridion 2011?

I'm using Tridion 2011's Event System to perform some additional actions when un-publishing components. I'm using code found here to publish a related component.

I'm registering my event handler as follows:

EventSystem.Subscribe<Component, UnPublishEventArgs>(
    RemoveAndRepublish, EventPhases.Initiated);

... and my handler method is as follows:

public void RemoveAndRepublish(Component cmp, UnPublishEventArgs args, 
                               EventPhases phase)
{
    // ... code to locate related component, and perform required actions...

    var instruction = new PublishInstruction(cmp.Session)
    {
        DeployAt = DateTime.Now,
        RenderInstruction = new RenderInstruction(cmp.Session)
        {
            RenderMode = RenderMode.Publish
        },
        ResolveInstruction = new ResolveInstruction(cmp.Session)
        {
            IncludeComponentLinks = true
        },
        RollbackOnFailure = true,
        StartAt = DateTime.MinValue
    };

    var target = args.Targets.FirstOrDefault();

    PublishEngine.Publish(new[] {related}, instruction, new[] {target});
}

My problem is that the UnPublishEventArgs.Targets property is an IList<PublishingTarget>, which at runtime turns out to be a List<TargetType>, and I need to get a PublicationTarget object to be able to call PublishEngine.Publish(...).

My question is: is there a way to get the current (un-)PublicationTarget from an UnPublish event?

Can anyone offer any help?

like image 508
James Simm Avatar asked Jan 31 '13 16:01

James Simm


1 Answers

You will need to figure out the PublicationTarget(s) yourself from the TargetType(s). Based on the TargetType and the Publication of the item you will need to iterate through the PublicationTargets to see if they allow your Publication to publish to them. This will (eventually) give you a list of PublicationTargets. Unfortunately that is quite a lot of work :(

All that said, do you really need the PublicationTarget? If you need to republish or un-publish items from the same Publication/Target combination, you should be able to parse the same TargetType array to the PublishEngine.Publish(...) method.

When I search the docs, I see the following override on PublishEngine():

PublishEngine.Publish Method (IEnumerable<(Of <(<'IdentifiableObject>)>)>, PublishInstruction, IEnumerable<(Of <(<'TargetType>)>)>, PublishPriority)

public static ICollection<PublishTransaction> Publish(
    IEnumerable<IdentifiableObject> items,
    PublishInstruction publishInstruction,
    IEnumerable<TargetType> targetTypes,
    PublishPriority priority
)
like image 149
Chris Summers Avatar answered Dec 15 '22 23:12

Chris Summers