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

1

7

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?

Aveyron answered 31/1, 2013 at 16:33 Comment(0)
I
7

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
)
Inmate answered 31/1, 2013 at 16:39 Comment(2)
OK, the 2nd paragraph sounds more promising... could you give me an example? The PublishEngine.Publish method takes an IEnumerable<PublicationTarget> not IEnumerable<PublishingTarget> .Aveyron
Doh, totally didn't see the overload of PublishEngine.Publish() that takes an array of TargetType. Thanks!Aveyron

© 2022 - 2024 — McMap. All rights reserved.