Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Create project specific datasource templates for your SXA tenant so you can extend them with custom fields - Promo on Showcase example
###### user settings start here
# Path to the tenant that you want to update
$tenantTemplatesPath = "master:\templates\Project\Showcase"
# Site that should be upgraded to the new template - ise asterisk to upgrade multiple sites.
$sitePath = "master:\content\Showcase\*"
# Path to the component for which you want to introduce your own data source
$componentPath = 'master:\layout\Renderings\Feature\Experience Accelerator\Page Content\Promo'
# Path to the feature or foundation folder that contains the templates
$featurePath = "Feature/Experience Accelerator/Page Content"
# templates to be cloned
$datasourceTemplateName = "Promo"
$datasourceFolderTemplateName = "Promo Folder"
###### user settings end here
# The function creates a Template in the project based on a template in either Foudation or Feature layer
function Assert-FeatureTemplateInProject {
[String[]] $TenantTemplateFolder,
[String[]] $FeatureTemplatePath,
[String[]] $FeatureTemplateName
# does the template exist?
$projectTemplate = Get-Item "$tenantTemplateFolder\$featureTemplateName" -ErrorAction SilentlyContinue
#if we haven't created tenant events yet - we need to do it
if ($projectTemplate -eq $null){
# create template
$projectTemplate = New-Item "$tenantTemplateFolder\$featureTemplateName" -ItemType "System/Templates/Template"
# add base feature template
Add-BaseTemplate -Item $projectTemplate -Template "$featureTemplatePath/$featureTemplateName"
# copy template icon from the base template
$projectTemplate.__Icon = (Get-Item "master:\templates\$featureTemplatePath/$featureTemplateName").__Icon
#create standard values if they don't exist
[Sitecore.Data.Items.TemplateItem]$templateItem = $projectTemplate
if($TemplateItem.StandardValues -eq $null){
$standardValues = $TemplateItem.CreateStandardValues()
# return the template
return $projectTemplate
# The function adds a template to the Insert options of another template on Standard values
function Add-ToStandardValues {
[Item] $Template,
[Item[]] $InsertTemplates
# let's get the standard values
[Sitecore.Data.Items.TemplateItem]$templateItem = Get-Item "$($Template.ProviderPath)"
#if we haven't created project calendar event yet - we need to do it
if($TemplateItem.StandardValues -eq $null){
$standardValues = $TemplateItem.CreateStandardValues()
$standardValues = Get-Item "$($Template.ProviderPath)\__Standard Values" -ErrorAction SilentlyContinue
$standardValues.__Masters = $InsertTemplates
# copy templates to project
$datasourceTemplate = Assert-FeatureTemplateInProject -TenantTemplateFolder $tenantTemplatesPath -FeatureTemplatePath $featurePath -FeatureTemplateName $datasourceTemplateName
$datasourceFolderTemplate = Assert-FeatureTemplateInProject -TenantTemplateFolder $tenantTemplatesPath -FeatureTemplatePath $featurePath -FeatureTemplateName $datasourceFolderTemplateName
# set insert options
Add-ToStandardValues -Template $datasourceFolderTemplate -InsertTemplates $datasourceTemplate
# replace the data source template with project specific
Get-ChildItem "$sitePath\Data" -Recurse | ? { $_.TemplateName -eq $datasourceTemplateName } |
% { Set-ItemTemplate -Item $_ -TemplateItem $datasourceTemplate;}
# replace the data source folder template with project specific
Get-ChildItem "$sitePath\Data" -Recurse | ? { $_.TemplateName -eq $datasourceFolderTemplateName } |
% { Set-ItemTemplate -Item $_ -TemplateItem $datasourceFolderTemplate; $_.__Masters = @($datasourceFolderTemplate, $datasourceTemplate); }
# get component
$component = Get-Item $componentPath
# Create datasource configuration for the component
$datacourceConfigRoots = @() + (Get-Item "$sitePath\Settings" | %{ Get-ChildItem $_.ProviderPath -Recurse } |
? { Test-BaseTemplate -Item $_ -TemplateItem (Get-Item master: -ID "$([Sitecore.XA.Foundation.LocalDatasources.Templates+DatasourceConfigurations]::ID)") } )
foreach($datacourceConfigRoot in $datacourceConfigRoots)
if(Test-Path "$datacourceConfigRoot\$($component.Name)"){
Write-Host "Configuration Exists - aborting"
$datasourceConfiguration = New-Item -Path $datacourceConfigRoot.ProviderPath -Name "$($component.Name)" -ItemType ((Get-Item master: -ID "$([Sitecore.XA.Foundation.LocalDatasources.Templates+DatasourceConfiguration]::ID)").Paths.Path)
$datasourceConfiguration.DatasourceTemplate = $datasourceTemplate.Paths.Path
$datasourceConfiguration.CompatibleRenderings = $component

This comment has been minimized.

Copy link

@Refactored Refactored commented Sep 29, 2017

Thanks Adam for this, very helpful. Minor change/suggestion to this script:

# set insert options
Add-ToStandardValues -Template $datasourceFolderTemplate -InsertTemplates $datasourceFolderTemplate
Add-ToStandardValues -Template $datasourceFolderTemplate -InsertTemplates $datasourceTemplate

In the example above, this ensures that child Promo Folders and Promos are set via Insert Options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment