Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the user who initiated a publish action in a SDL Tridion C# TBB

From a C# TBB used by a Modular Page Template in SDL Tridion 2011, is it possible to access the User object who initiated the Publishing action?

Looking at the TOM.NET 6 Programmers Reference Guide, it seems that the property I need is the Creator property of the PublicationTransaction object, but I can’t find a way to access that from a C# TBB, I don’t see an obvious way to get the current PublicationTransaction from the engine or package objects, and I can only find a way to get a list of PublicationTransaction objects using the PublishEngine object.

Any advice would be greatly appreciated.

like image 401
GourmetCMS Avatar asked Jul 27 '12 00:07

GourmetCMS


1 Answers

Have a look at these two blog posts from Mihai Cadariu:

  • How to look up the current Publish Transaction (based on a trick from Chris Summers)
  • Create a new Publish Transaction based on an existing one

With those two you should be able to find what you need.

The basic function you need in your TBB is this:

public PublishTransaction GetPublishTransaction(Engine engine)
{
    String binaryPath = engine.PublishingContext.PublishInstruction.
                                         RenderInstruction.BinaryStoragePath;
    Regex tcmRegex = new Regex(@"tcm_\d+-\d+-66560");
    Match match = tcmRegex.Match(binaryPath);

    if (match.Success)
    {
        String transactionId = match.Value.Replace('_', ':');
        TcmUri transactionUri = new TcmUri(transactionId);
        return new PublishTransaction(transactionUri, engine.GetSession());
    }

    return null;
}

It might also be worth noting that the property engine.PublishingContext.PublishInstruction.RenderInstruction.BinaryStoragePath will return something different when rendering the coder in PreviewMode or from the Template Builder compared to when the code is running in the Publisher. To see the PublishTransaction URI in the BinaryStoragePath, you must attach your Visual Studio TBB Debug Project to the TcmPublisher.exe process in order for there to actually be a PublishTransaction object present, otherwise the BinaryStoragePath will just contain a generic path like ../preview.

like image 140
5 revs, 2 users 51% Avatar answered Nov 06 '22 06:11

5 revs, 2 users 51%