Adding a New 'Default Finish Action' in Tridion

Introduction

The “Default Finish Action” template building block provides a central location for executing C# code fragments and other finishing steps within the Tridion file-publishing process. For example, upon publishing a file, the default finish action may insert variables, process data, optimize layout of resulting HTML, etc. In this tutorial, we describe how to add a new action to the default finish action template building block, providing a new publish step, sharable by all page and component templates.

SDL Tridion Tutorial: Adding a New Default Finish Action

Who’s the “Real” Default Finish Action?

Within a standard blueprint architecture in Tridion, there can be multiple default finish action template building blocks (one for each publication), but only one will be inherited from the base publication. This means each child publication will share the same Default Finish Action template building block inherited from the Empty Base publication. This is important to note, as we’ll be required to localize a copy of the default finish action within our designated blueprinting publication (Design Base) to share our custom changes with child publications.

Adding a New Action to “Default Finish Action”

  1. Select the publication Design Base

  2. Open the folder Default Templates

  3. Verify the Template Building Block Default Finish Actions is localized (verify the column “From Publication” is empty). If it is not, localize the block to separate it from Empty Base (the base Tridion Blueprinting publication, if one exists in your Tridion instance) by right-clicking it and selecting BluePrinting->Localize.

  4. Create a newTemplate Building Block as a C# Fragment in the publication, Design Base/Template Building Blocks.

  5. For Name, enter “Demo Code Block”.

  6. Click the “Source” tab.

  7. For Template Type, select “C# Code Fragment”.

  8. For the C# code, enter any valid C# code fragment to save in the template building block, such as the following code, which appends an HTML comment to the bottom of the page.

1
2
3
4
5
6
//
// Append the DateTime to the bottom of the page.
//
string value = package.GetValue("Output");
value += "<!—Last published: " + DateTime.Now + " -->";
package.PushItem("Output", package.CreateHtmlItem(value));
  1. Click Save and Close.

  2. Run the SDL Template Builder Tool.

  3. Click Open, navigate to the publication Design Base/Building Blocks/Default Templates and select “Default Finish Actions“ to open. Once opened, you should see several C# Template Building Blocks in the center pane, including “Publish Binaries in Package”, “Link Resolver”, “Target Group Personalization”, “Cleanup Template”, and “Convert Xml to Html”.

  4. Locate the new C# fragment template building block in the left-side pane, within “Template Building Blocks“. If you do not see the file created in step 5 above (“Demo Code Block”), click View->Reload Building Blocks to refresh the view. Drag the block “Demo Code Block” to the end of the list in the middle pane.

  5. Save the Default Finish Actions template building block.

  6. Test the result by opening your desired Page Template: click Open in the Template Builder Tool, select the desired file (Publication/Building Blocks/Page Templates/template.html), verify “Default Finish Actions” exists in the center white pane. If it does not, locate “Default Finish Actions” along the left-pane under “Default Templates” and drag it to the end of the center pane, click Save._Note, you can also verify that the page template includes “Default Finish Actions”in the Tridion UI by opening the Page Template in Tridion, clicking the “Source” tab, and looking for “Default Finish Actions” in the XML content.

  7. Run the Page Template in the Template Builder Tool by clicking Debug->Run and verify the resulting output in the “Text” tab of the results window, located on the right-side of the tool under “Item View”. There are three tabs, Binary, Text, HTML. Click “Text” to view the text output (also considered the HTML source of the resulting page). If you scroll to the bottom of the output, you should see what our C# Code Fragment added, which is an HTML comment containing the date and time:

<!—Last published: 10/29/2015 4:22:30 PM –>

You can also preview any page within a purple structure group folder in Tridion and see the same modification to the page’s content. For example, right-click any of your other pages in the same publication (within the purple structure group folders) and select Preview. Click the “Source” tab and you should see the HTML comment added by our code block “<!—Last published: 10/29/2015 4:22:30 PM –>”. All pages that contain the Default Finish Action in their page template will now include this change.

The Result

After localizing and saving the Default Finish Action within the Design Base publication, all child publications which inherit from Design Base will now contain the updated Default Finish Action. Upon publishing any page or component template that includes the Default Finish Action template building block, the newly added action will execute and update the resulting output for the target file. In the above example, an HTML comment was appended to the bottom of the HTML source code, containing “<!– Last published 10/29/2015 –>”.

Another example of a C# Code Block is shown below. This particular example replaces multiple blank lines within the HTML source with a single blank line. This can help provide a more readable and search friendly HTML source for all of your pages. Example code is shown below.

1
2
3
4
5
6
7
//
// Replace multiple blank lines with a single blank line.
// http://social.msdn.microsoft.com/Forums/en-US/regexp/thread/2cb4e796-0edf-4c13-ba12-ebf59ca0ee87
//
string value = package.GetValue("Output");
value = System.Text.RegularExpressions.Regex.Replace(value, @"(?<=(?:\r?\n){2}|\A)(?:\r?\n)+", "");
package.PushItem("Output", package.CreateHtmlItem(value));

About the Author

This article was written by Kory Becker, software developer and architect, skilled in a range of technologies, including web application development, machine learning, artificial intelligence, and data science.

Share