INDIVIRTUAL - TECHNISCH PARTNER IN DIGITALE DIENSTVERLENING

Adding publications to Tridion via the core service

November 4, 2014

Adding publications to Tridion via the core service

A few weeks ago I was privileged to attend the SDL Tridion MVP summit in Evora, Portugal. Needless to say, it was a very interesting few days, and as usual, we spent quite some time on some technical subjects. The most prominent of these was the Tridion Reference Implementation (TRI), probably because a couple of the MVPs (Will Price and Bart Koopman) had been involved in creating it. We all installed it and kicked the tyres a bit, although to be fair, I think I’d need more time to do it justice. I was, however, delighted to discover that the import script for the reference implementation had used (and credited!) a few of the core service powershell scripts that I blogged about here some months ago.

Specifically - they made use of the core service to set rights and permissions in Tridion, and also to access the new Content Porter core service API.

This is where PowerShell scripting comes into its own. Although it’s not always very obvious how to tackle some tasks, if you have a library of your own functions to use, or better still if you can use something that someone else has published to the community, you can put together a useful script fairly quickly.

Reading a little further into the import script, I realised that at one point it would be nice to have the script create some publications, but the TRI team hadn’t done this. Obviously, I thought, they need me to post some sample code to make this easier. (Seriously, I’ll get in touch with the guys to see if they want it in their script. We’ll see, and in the meantime, you can make your own version if it suits you.)

So here goes. The object here is to create three publications which will form a BluePrinting chain. Actually, TRI only needs two publications, which have default titles of “100 Master”, and “400 Example Site”, but we’ll stick to common practice and also give it an empty master. Actually, I don’t want to use the defaults, because on my system I want to easily see which publications belong to TRI, so I’ll be using the names “TRI 000 Empty”, “TRI 100 Master”, and “TRI 400 Example Site”. Of course, if you integrated this into the TRI import script, you’d just pass the existing parameters through.

# Depends on Tridion Powershell Modules (http://code.google.com/p/tridion-powershell-modules/)

function createPublication {
	Param(
		[parameter(Mandatory=$true)]
		[ValidateNotNullOrEmpty()]
		$core, 
		[parameter(Mandatory=$true)]
		[ValidateNotNullOrEmpty()]
		[string]$title, 
		[string]$key, 
		[string[]]$parents,
		[switch]$Passthru
	)
	write-host "Creating publication $title"
	$newPublication = $core.GetDefaultData([Tridion.ContentManager.CoreService.Client.ItemType]::Publication,"",$null)
	$newPublication.Title = $title
	if ($key -eq [string]::Empty){
		$newPublication.Key = $title
	}
	else {
		$newPublication.Key = $key
	}
	foreach ($parent in $parents){
		$link = new-object Tridion.ContentManager.CoreService.Client.LinkToRepositoryData
		if ($parent -match "^tcm:"){
			$link.IdRef = $parent
		} elseif ($parent -match "^/webdav"){
			$link.WebDavUrl = $parent
		} else {
			continue
		}
		$newPublication.Parents += $link
	}
	if ($Passthru){
		$core.Create($newPublication, (new-object Tridion.ContentManager.CoreService.Client.ReadOptions))
	}
	else {
		$core.Create($newPublication,$null)
	}
}

function createStructureGroup($core, [string]$parentId, [string]$title, [string]$directory, [switch]$Passthru){
	write-Host "Creating Structure Group $title"
	$newStructureGroup = $core.GetDefaultData([Tridion.ContentManager.CoreService.Client.ItemType]::StructureGroup, $parentId, $null)
	$newStructureGroup.Title = $title
	$newStructureGroup.Directory = $directory
	if ($Passthru){
		$core.Create($newStructureGroup, (new-object Tridion.ContentManager.CoreService.Client.ReadOptions))
	}
	else {
		$core.Create($newStructureGroup, $null)
	}
}

function createImportPublications($core, $chainMasterTitle="000 Empty", $masterTitle="100 Master", $webTitle="100 Example Site", $rootStructureGroup="Home") {

	$chainMasterPub = createPublication $core $chainMasterTitle -Passthru
	$rsg = createStructureGroup $core $chainMasterPub.Id $rootStructureGroup $rootStructureGroup -Passthru
	$masterPub = createPublication $core $masterTitle -parents @($chainMasterPub.Id) -Passthru
	createPublication $core $webTitle -parents @($masterPub.Id) 
}


$core = Get-TridionCoreServiceClient
createImportPublications $core "TRI 000 Empty" "TRI 100 Master" "TRI 400 Example Site"

Obviously, this technique is useful in other scenarios than the TRI. I have a very similar script which I use if I need to set up a standard blueprint very quickly, perhaps for testing something new if I don’t want it to impact other work on the same server. Anyway - I hope this triggers you to think of a couple of things you could easily automate in your own life.

And, of course, don’t forget to check out the Tridion Reference Implementation. It’s a great showcase for a lot of interesting techniques. It’s based on DD4T, and also has its own front-end build infrastructure. Nice job, guys.

 

Dominic Cronin

Dominic Cronin