Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check publishing or rendering context with TOM.NET?

Tags:

tridion

SDL Tridion's Content Manager templating API (TOM.NET) offers ways to detect publishing or rendering context.

Use Cases

  • Present debugging information to a specific environment (e.g. TCM Uris only on Staging)
  • Show different markup in Preview (e.g. show a link to the published page)
  • Show different author-able fields in Experience Manager or SiteEdit

I've seen and tried a few examples, but after following a chat between colleagues Stan and Eric, I want to make sure I follow for TOM.NET (6.1 / Tridion 2011).

Scenarios

  1. Publishing to a specific Publication Target (typically "Live" and "Staging")
  2. Content Manager Explorer (CME) Preview
  3. Session Preview rendering for Experience Manager (XPM)
  4. (Added) Template Builder

1. Publishing to a Target (or from a Publication)

Tridion.ContentManager.Publishing.PublishEngine.GetPublishInfo(IdentifiableObject item)

Item would be a page or component. This returns a collection of PublishInfo objects, which includes PublicationTarget to confirm where you're publishing to.

Tridion.ContentManager.Templating.PublishingContext.PublicationTarget has PublicationTarget as well.

2. CME Preview

PublicationTarget is null, which makes sense because you don't have a Publication Target . :-)

3. Session Preview

Use the RenderMode Enum under Tridion.ContentManager.Publishing, which has:

  • 'Publish' (0)
  • 'PreviewStatic' (1)
  • 'PreviewDynamic' (2)

PublicationTarget wouldn't be null for Session Preview, which isn't really publishing.

4. (Added) Template Builder

?

Alexander Klock also describes some related examples which cover most of this except CME Preview.

Question(s)

  • Am I missing any scenarios? Publish to a specific publication target, regular preview, and XPM session preview?

  • How should I avoid hard-coding PublicationTargets (e.g. is it better to check the string values instead of TCM Uris)?

  • Update: added Template Builder to the list per Vikas's answer, how do I know I'm rendering within Template Builder?

like image 974
Alvin Reyes Avatar asked Feb 17 '23 17:02

Alvin Reyes


2 Answers

You really need a tl;dr on this question...

Here's what I know:

Template Builder

Publication target is null, RenderMode is PreviewDynamic

CME Preview

Publication Target ID is tcm:0-0-0 (or TcmUri.UriNull), RenderMode is PreviewDynamic

Session Preview

Publication Target ID is the real target ID, RenderMode is PreviewDynamic

Publish

Publication Target ID is the real one, RenderMode is Publish

EDIT

Here's some sample code I wrote recently to determine the Current mode.

private CurrentMode GetCurrentMode()
{
    RenderMode renderMode = _engine.RenderMode;
    if (renderMode == RenderMode.Publish) return CurrentMode.Publish;


    if (renderMode == RenderMode.PreviewDynamic)
    {
        if (_engine.PublishingContext.PublicationTarget == null) return CurrentMode.TemplateBuilder;
        PublicationTarget target = _engine.PublishingContext.PublicationTarget;
        if (target.Id.Equals(TcmUri.UriNull)) return CurrentMode.CmePreview;
        return CurrentMode.SessionPreview;
    }
    return CurrentMode.Unknown;
}

private enum CurrentMode
{
    TemplateBuilder,
    CmePreview,
    SessionPreview,
    Publish,
    Unknown
}
like image 98
Nuno Linhares Avatar answered Mar 04 '23 13:03

Nuno Linhares


You presented a very good view of complete publishing/preview model. Here are my thoughts..

Are we missing any scenarios? 

I think you covered everything expect the template builder case, which is similar to CME preview where we get publishing target as null but can be used to check different condition so important for debugging purpose.

How should I avoid hard-coding PublicationTargets  

Yes we should never use tcm uri in any code, as you suggested we could use name and even name can be configured in respective config files for that program.

Also may not be relevant here, its always good to have separate target for Tridion UI edting other than staging. Both can be configured on same server with two deployer's. One could be staging.yoursite.com and other could be tridionui.yoursite.com

Thanks..

like image 23
vikas kumar Avatar answered Mar 04 '23 13:03

vikas kumar