I was struggling a while back to retrieve the current Publish Transaction from template code. This is the Publish Transaction that is currently being executed in the Publisher. I needed it to identify who sent the current item to publish and with which priority. I found out it wasn't easy to retrieve the current Publish Transaction. In fact, it was impossible to identify it with absolute certainty.
I tried looking at the Publishing Queue and pick the transaction 'In Progress' or with status Rendering. However, this doesn't work reliably due to possible concurrent rendering threads or even multiple Publishers, so it is impossible to know for sure which exactly is the current Publish Transaction.
Then the following hack came to light thanks to a post by Chris Summers. And what a hack it is indeed: take the Binary storage path of the RenderInstruction and extract the Publish Transaction TCMURI from it.
The Binary storage path looks something like C:\Temp\tcm_0-164479-66560.Content\Binaries, so I can apply a Regular Expression to it to extract the TCMURI.
public PublishTransaction GetPublishTransaction(Engine engine)
I tried looking at the Publishing Queue and pick the transaction 'In Progress' or with status Rendering. However, this doesn't work reliably due to possible concurrent rendering threads or even multiple Publishers, so it is impossible to know for sure which exactly is the current Publish Transaction.
Then the following hack came to light thanks to a post by Chris Summers. And what a hack it is indeed: take the Binary storage path of the RenderInstruction and extract the Publish Transaction TCMURI from it.
The Binary storage path looks something like C:\Temp\tcm_0-164479-66560.Content\Binaries, so I can apply a Regular Expression to it to extract the TCMURI.
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;
}
In the meantime, I filed an enhancement request on this topic.
Comments