10

We are using Tridion 2011 SP1. When a component is being published we are updating a metadata field of component in our component template.

The code which updates the component metadata field is working fine but we found the updated values are not in the version of the component getting published. Rather, values from the previous version of component are getting published.

When we open the component in the Tridion GUI, the component shows the values have been updated by the component template.

What changes we have to make to ensure the publishing code is working with the latest (updated by component template) version of component?

2
  • 3
    Just curiosity, but why would you want to change a field during publish time?
    – Kah Tang
    Mar 18, 2013 at 19:59
  • Maybe you can try to have a C# TBB in your package which as per your need changes the metadata value and generate the Rendered Presentation with the changed metadata. Mar 19, 2013 at 13:05

3 Answers 3

11

If you absolutely must update a metadata field (or a regular field) of the component when publishing, you can subscribe to the pre-publish events on the component and add logic to make the update BEFORE the publishing actually starts. This does mean your logic for updating the field needs to move out of the component template and into the event system code.

If you haven't done event system work before here are three code blog posts to read:

And here's a general code framework you might use for this:

using System;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.Extensibility.Events;

namespace TridionEventSystem
{
    [TcmExtension("MyTridion2011EventSystem")]  
    public class EventSystem : TcmExtension
    {
        public EventSystem()
        {
            Subscribe();
        }

        public void Subscribe()
        {
            EventSystem.Subscribe<Component, PublishEventArgs>(OnComponentPublishPre, EventPhases.Initiated);
        }

        private static void OnComponentPublishPre(Component component, PublishEventArgs args, EventPhases phase)
        {
            if (component.Schema.Title.Equals("MySchema"))
            {
                // Code to update your component
            }
        }
    }
}

And in Tridion.ContentManager.Config make sure to add a reference to your resulting dll:

<extensions>
    <add assemblyFileName="c:\Program Files (x86)\Tridion\Bin\MyDll.Tridion.EventSystem.dll" />
</extensions>
8

The problem with having templates manipulate the content include:

  • Multiple templates have to make the exact same change or risk over-writing each other's changes.
  • Authors can change fields they're not really managing.

Adding this to the component presentations via templating is safer. But if you're just saving publishing information, consider AppData instead.

Otherwise, we have three basic places [Julian Wraith] (and six extension points [Bart Koopman]) to add or manipulate content.

  • Baked in the Content Management System (appropriate if data is known even before publish and authors should still be able to update it)
  • Fried in Content Delivery during render time and/or upon request (code, script, whatever). And in-between the two:
  • Half-baked during Publish via templating and the package Nuno refers to. He has an article on ways TOM can integrate with other systems during publish.
5

That's the expected behavior - and it has been that way ever since I remember. By the time you change the component it's too late - Tridion already loaded the component in memory, and since Template rendering is a read-only operation by design, Tridion will not bother re-loading it from the Database. It's called Cache.

If you really want to change a value of a component at publish time consider changing its value in the package, not in the database. This has the advantage that 1) you don't need to change Tridion so that it enables write-on-templates and 2) you don't store data in your component that you probably really don't need.

As Kah suggests, maybe you can expand on why you want to change the component at publish time and we may be able to help you better.

Your Answer

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

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