4
votes

I'm new to Sitecore PowerShell (and PowerShell in general), and I would like to generate a script that adds a Template as a Base Template to a list of ChildItem Templates under a certain context folder.

Since __Base template is a standard value and not a regular Field, I'm not sure if there is a syntax available to append a GUID to the multilist field that it uses (pipe delimited).

Here's what I've tried

$master = Get-Database master;
$translationTemplate = $master.Templates["Path/To/Template/Needed/Translatable"];
$calloutTemplate = $master.Templates["Path/To/Example/Callout"];

#$translationTemplate;

$calloutTemplate += $translationTemplate;

I'm able to get all of the children recursively with Get-ChildItem -recursive, but for a test run, I just want to try it on one Template Item called Callout. Here's a view of the $calloutTemplate, showing that it has a BaseTemplates section and a Fields section (which should include __Base template):

enter image description here

If there is no solution in Powershell for Sitecore, would Sitecore Rocks Query analyser CRUD operations possibly work?

1

1 Answers

6
votes

Basically the script you're looking for is:

$templateToExtend = Get-Item 'master:/templates/Path/To/Example/Callout'
$templateToAdd= Get-Item 'master:/templates/Path/To/Template/Needed/Translatable'

# Make sure we're not adding it again if it's already there
if(-not ($templateToExtend."__Base template" -match "$($templateToAdd.Id)"))
{
    "Adding '$($templateToAdd.Name)' to '$($templateToExtend.Name)' Base templates"
    $templateToExtend."__Base template" = "$($templateToExtend.'__Base template')|$($templateToAdd.Id)" 
}

# Just to verify it got added:
$templateToExtend."__Base template"

However just that doesn't really give PowerShell justice. The power of it is that you can now turn this snippet into a function that can be piped into. You can do this easily following Michael's tutorial on his blog