Tableau Migration SDK 5.1.1
  • Articles
  • Code Samples
  • Python API Reference
  • C# API Reference
Show / Hide Table of Contents
  • Filters
    • Filter projects by name
    • Filter users by SiteRole
    • Filter Custom Views by 'Shared' flag
  • Mappings
    • Username email
    • Rename projects
    • Change projects
  • Transformers
    • Add tags to content
    • Encrypt Extracts
    • Adjust 'Start At' to Scheduled Tasks
    • Change default users for Custom Views
    • Action URL XML Transformer
  • Post-Publish Hooks
    • Update permissions
  • Bulk Post-Publish Hooks
    • Bulk logging
  • Batch Migration Completed Hooks
    • Batch migration logging
  • Migration Action Completed Hooks
    • Migration action logging

Sample: Action URL XML Transformer

This sample demonstrates how to read and write XML to update the workbook files. XML transformers should use the XmlContentTransformerBase base class that handles parsing the file XML and saving the modified XML back to the file to be published.

XML transformers require additional resource overhead to execute, so care should be taken when developing them.

  • Due to encryption, XML transformers require the file to be loaded into memory to modify. For large files, such as those with large extracts, this can require significant memory.
  • Python XML transformers each require extra processing as the parsed XML is converted from a .NET representation to a Python representation. For files with large XML content this can require significant time and memory. Combining multiple Python XML transformers can minimize this impact.

In general, resource overhead can be mimized by implementing the NeedsXmlTransforming/needs_xml_transforming method, which allows the transformer to use the metadata of the content item to determine whether the XML file needs to be loaded.

XML transformers are provided with "raw" XML and no file format validation is performed by the SDK. Care should be taken when modifying workbook or other files that the changes do not result in content that is valid XML but invalid by the file format. File format errors can lead to migration errors during publishing, and can also cause errors that are only apparent after the migration is complete and reported success.

  • Python
  • C#

Transformer Class

To update action URLs in Python, you can use the following transformer class:

from xml.etree import ElementTree
from tableau_migration import (
    IPublishableWorkbook,
    XmlContentTransformerBase
)

class ActionUrlXmlTransformer(XmlContentTransformerBase[IPublishableWorkbook]):
    
    def needs_xml_transforming(self, ctx: IPublishableWorkbook) -> bool:
        # Returning false prevents the transform method from running.
        # Implementing this method potentially allows workbooks to migrate
        # without loading the file into memory, improving migration speed.
        return True

    def transform(self, ctx: IPublishableWorkbook, xml: ElementTree.Element) -> None:
        # Changes to the XML are saved back to the workbook file before publishing.
        for action_link in xml.findall("actions/*/link"):
            action_link.set("expression", action_link.get("expression").replace("127.0.0.1", "testserver"))

Registration

plan_builder.transformers.add(ActionUrlXmlTransformer)

See hook registration for more details.

Transformer Class

In C#, the transformer class for adjusting action URLs is implemented as follows:


public class ActionUrlXmlTransformer : XmlContentTransformerBase<IPublishableWorkbook>
{
    protected override bool NeedsXmlTransforming(IPublishableWorkbook ctx)
    {
        /*
         * Returning false prevents TransformAsync from running.
         * Implementing this method potentially allows workbooks to migrate without 
         * loading the file into memory, improving migration speed.
         */
        return true;
    }

    public override Task TransformAsync(IPublishableWorkbook ctx, XDocument xml, CancellationToken cancel)
    {
        // Changes to the XML are saved back to the workbook file before publishing.
        foreach (var actionLink in xml.XPathSelectElements("//actions/*/link"))
        {
            actionLink.SetAttributeValue("expression", actionLink.Attribute("expression")?.Value?.Replace("127.0.0.1", "testserver"));
        }

        return Task.CompletedTask;
    }
}

Registration

To register the transformer in C#, follow the guidance provided in the documentation.

_planBuilder.Transformers.Add<ActionUrlXmlTransformer, IPublishableWorkbook>();

Dependency Injection

Learn more about dependency injection here.

services.AddScoped<ActionUrlXmlTransformer>();
  • Edit this page
In this article