Dave Simione
Dave Simione

Reputation: 1441

Tridion 2009 - Publish another Component from a Component Template

First, the overall description:

There are two Component Templates, NewsArticle and NewsList. NewsArticle is a Dreamweaver Template, and is used to display the content of a news article. NewsList is an xml file that contains aggregated information about all of the news articles.

Currently, a content author must publish the news article, and then re-publish the newslist to regenerate the xml.

Problem:

I have been tasked with having the publish of a news article also regenerate and publish the newslist. Through C#, I am able to retrieve the content of the newslist component, generate the updated xml from the news article, and merge it into the xml from the newslist. I am running into trouble getting the newslist to publish.

I have limited access to documentation, but from what I do have, I believe using the static PublishEngine.Publish method will allow me to do what I need. I believe the first parameter (items) is just a list that contains my updated newslist, and the second parameter is a new PublishInstruction with the RenderInstruction.RenderMode set to Publish. I am a little lost on what the publicationTargets should be.

Am I on the right track? If so, any help with the Publish method call is appreciated, and if not, any suggestions?

Upvotes: 2

Views: 381

Answers (2)

Nickoli Roussakov
Nickoli Roussakov

Reputation: 3409

Like Quirijn suggested, a broker query is the cleanest approach.

In a situation if a broker isn't available (i.e. static publishing model only) I usually generate the newslist XML from a TBB that adds the XML as a binary, rather than kicking off publishing of another component or page. You can do this by calling this method in your C# TBB:

engine.PublishingContext.RenderedItem.AddBinary(
  Stream yourXmlContentConvertedToMemoryStream, 
  string filename, 
  StructureGroup location, 
  string variantId, 
  string mimeType)

Make the variantId unique per the newslist XML file that you create, so that different components can overwrite/update the same file.

Better yet, do this in a Page Template rather than Component Template so that the news list is generated once per page, rather than per component (if you have multiple articles per page).

Upvotes: 6

Chris Summers
Chris Summers

Reputation: 10163

You are on the right tracks here with the engine.Publish() method:

PublishEngine.Publish(
    new IdentifiableObject[] { linkedComponent },
    engine.PublishingContext.PublishInstruction,
     new List() { engine.PublishingContext.PublicationTarget });

You can just reuse the PublishInstruction and Target from the current context of your template. This sample shows a Component, but it should work in a page too.

One thing to keep in mind is that this is not possible in SDL Tridion 2011 SP1, as the publish action is not allowed out of the box due to security restrictions. I have an article about this here http://www.tridiondeveloper.com/the-story-of-sdl-tridion-2011-custom-resolver-and-the-allowwriteoperationsintemplates-attribute

Upvotes: 5

Related Questions