Using a script to enable tag history and setting the database

I am trying to save time by running a script to change tags by enabling history and set a database. I am unable to figure out how to set the database.

def enable_history_for_folder(base_path):
    tags = system.tag.browse(base_path)
    database = "SFISQL04_MSSQL"
    for tag in tags:
        full_path = base_path + "/" + tag['name']
        
        if tag['hasChildren']:
            # Recurse into subfolders
            enable_history_for_folder(full_path)
        else:
            try:
                # Get tag configuration
                config = system.tag.getConfiguration(full_path, True)[0]

                # Set history properties
                config["HistoryEnabled"] = True
                config["StorageProvider"] = database  # Replace with your actual history provider name

                # Write the new config
                system.tag.configure(base_path, [config], "m")

                print("Enabled history for:", full_path)
            except Exception as e:
                print("Failed to update:", full_path, "| Error:", e)

Post code, not pictures of code, it helps others while troubleshooting. Please see Wiki - how to post code on this forum. You can edit your post by clicking the pencil icon in the lower right.

Have you confirmed that the rest of your changes are being applied? Or is the script failing entirely? Have you confirmed that the history provider that you are specifying actually exists as a history provider in your gateway?

I updated my post. Yes, it is just the storage provider update that is not working. It will change the history enabled from false to true.

In that case I would double check that the history provider you are specifying actually exists. The Tags section in the Status tab of your gateway should show a list of your history providers. Also, confirm you are setting the correct key in the tag config.

If you can, manually set the provider you want on a single tag, then read that tag via system.tag.getConfiguration to see how the provider is shown in the tag config.

Because the property you are trying to update is actually called "HistoryProvider"

2 Likes

Thank you for the help

For future scripting: