7

What I need is to have the Publication Target of a current Page.

With the following code I get the Publication Target where the Page is published, but now I need to ensure that this Publication Target is the same as the Publication Target where the Page is publishing to.

ICollection<PublishInfo> publishInfo = PublishEngine.GetPublishInfo(page);
foreach (PublishInfo info in publishInfo)
{
    info.PublicationTarget.Id
}

I'm trying to avoid the situation that if you publish a Page from the same CMS into Staging and Live and you are showing the Pages just if they are published or not. Also Pages published on Staging will appear in Live.

Thanks in advance

2
  • What exactly do you mean with "the Publication Target were the Page is"? Also where are you using this code, is it at render time in a Template and what is it exactly for then? Commented Dec 17, 2013 at 9:09
  • What I mean is that if you are publishing a page to Live I need to know that the live Publication Target, but if I'm publishing to staging I need the Publication Target of staging.
    – ITB
    Commented Dec 17, 2013 at 9:12

1 Answer 1

10

In an Assembly Template Building Block you can determine the RenderMode and PublishingContext via the engine.

But before checking what Publication Target the current item is being published to, you need to know if you are actually being published, or if the render action of the Template is just a preview.

if (engine.RenderMode == RenderMode.Publish)
{
    PublicationTarget target = engine.PublishingContext.PublicationTarget;
}

From here onwards you can use target.Id or target.Title to check if it is your staging or live target that your item is being published to.

5
  • Hello Bart Koopman. Thanks for the quick answer. I tested it and it looks that works when I publish the page but I get an error with Template Builder, what is a pity because as you may now it is really usefull to debug Component Templates. Thanks
    – ITB
    Commented Dec 17, 2013 at 10:19
  • 1
    What error do you get in the Template Builder, the Template Builder should never pass the condition engine.RenderMode == RenderMode.Publish Commented Dec 17, 2013 at 10:24
  • Yes you are right. The problem is that I was trying to publish in the Template Builder debug log the Publication Target Id _log.Debug("Publication Target Id: " + this.engine.PublishingContext.PublicationTarget); So that's why I got the error. So there is any way to use this.engine.PublishingContext.PublicationTarget; without being in publishing time? Really thanks in advance Bart
    – ITB
    Commented Dec 17, 2013 at 10:29
  • 1
    Since the Template Builder is rendering, there is no Publication Target, so you can obviously NOT use engine.PublishingContext.PublicationTarget, if you want to debug this code, you need to attach to the publisher service, and debug when actually publishing Commented Dec 17, 2013 at 10:43
  • Ok, Bart. Thanks for all your comments. It was really usefull
    – ITB
    Commented Dec 17, 2013 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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