How to check the publication or display of context using TOM.NET?

The SDL Tridion Content Manager templating API (TOM.NET) offers ways to detect publication or rendering context.

Use cases

  • Present debugging information for a specific environment (for example, TCM Uris only at the stage)
  • Show another markup in preview mode (for example, show a link to a published page)
  • Show different fields available to authors in the Experience Manager or SiteEdit

I have seen and tried several examples, but after talking between colleagues Stan and Eric , I want to make sure that I follow TOM.NET (6.1 / Tridion 2011).

Scenarios

  • Publish to a specific publication purpose (usually "Live" and "Staging")
  • Content Manager Explorer (CME) Explorer Preview
  • Preview Rendering Session for Experience Manager (XPM)
  • (Added) Template Designer

1. Publication to the target (or from the publication)

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

The item will be a page or component. This returns a collection of PublishInfo objects that includes a PublicationTarget to validate where you are publishing.

Tridion.ContentManager.Templating.PublishingContext.PublicationTarget has a PublicationTarget .

2. CME preview

PublicationTarget is null , which makes sense because you have no purpose for publishing. :-)

3. Session Preview

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

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

PublicationTarget will not be null to preview a session that is not being published.

4. (Added) Template Builder

Alexander Klok also describes some related examples that cover most of them, besides CME preview.

Question (s)

  • Did I miss any scripts? Publish to targeted publication publication, regular preview, and preview of an XPM session

  • How can I avoid hard coding PublicationTargets (for example, it is better to check string values ​​instead of TCM Uris)?

  • Update: added a template to the list on Vikas answer , how do I know if I am rendering inside the Template Builder?

+4
source share
2 answers

You really need tl; dr on this subject ...

Here is what I know:

Template Designer

Publish Purpose - null, RenderMode - PreviewDynamic

CME Preview

Publishing Target Identifier: tcm: 0-0-0 (or TcmUri.UriNull ), RenderMode - PreviewDynamic

Session Preview

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

Publish

Publish landing page id is real, RenderMode publishes

EDIT

Here is an example of the 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 } 
+4
source

You have provided a very good overview of the full publish / preview model. Here are my thoughts.

 Are we missing any scenarios? 

I think that you all covered in the case of creating a template template that looks like a CME preview, where we get the publication of the target as null, but can be used to check the various states that are so important for the debugging target.

 How should I avoid hard-coding PublicationTargets 

Yes, we should never use tcm uri in any code, since you suggested that we could use the name and even the name can be configured in the corresponding configuration files for this program.

It may also not be appropriate here, it is always useful to have a separate goal for editing the Tridion UI, except for the setting. Both can be configured on the same server with two installers. Could be staging.yoursite.com and others could be tridionui.yoursite.com

Thanks..

+4
source