CreatingPublicationTargetsFromScript - TridionPractice/tridion-practice GitHub Wiki
Introduction
It is sometimes desirable to be able to drop your existing publication targets and create new ones in a predictable and repeatable way. An example of this is if you want to regularly copy your production database to the rest of your development/test environments, perhaps at the start of each development sprint. This script demonstrates a method of doing this, based on the idea of "script as configuration". In other words, the script itself contains the data that it needs to work. Here we've used JSON, but it could just as well be XML or another format.
Code
The script itself is in the repository here
Discussion
The data is laid out using JSON, giving us a very expressive way to model the actual data structure in Tridion using arrays of objects with named properties. Where the Tridion API expects XML, we just store a snippet of XML as a string in the JSON. To create the XML in the first place required some reverse-engineering, but based on the examples in the script you should be able to copy/paste/edit to create some data that meets your own needs. The rest of the properties follow the names used in the API.
The RecreatePublishingConfiguration function begins by looping through the data that specifies the desired publication targets. For each specified target, it checks if a target of the same name already exists, and if so, decommissions and then deletes the target before re-creating it. It's beyond the scope of the script to delete the published assets from your file system or re-initialise your broker database. Needless to say, in a real-world implementation, you would also wish to script these tasks.