Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Better Events List Script - Convert SXA Events templates into searchable items
$tenantTemplatesPath = "master:\templates\Project\Showcase"
$sitePath = "master:\content\Showcase\int"
function Assert-FeatureTemplateInProject {
[CmdletBinding()]
param(
[parameter(Mandatory=$true)]
[String[]] $TenantTemplateFolder,
[parameter(Mandatory=$true)]
[String[]] $FeatureTemplatePath,
[parameter(Mandatory=$true)]
[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
}
function Add-ToStandardValues {
[CmdletBinding()]
param(
[parameter(Mandatory=$true)]
[Item] $Template,
[parameter(Mandatory=$true)]
[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()
}else{
$standardValues = Get-Item "$($Template.ProviderPath)\__Standard Values" -ErrorAction SilentlyContinue
}
$standardValues.__Masters = $InsertTemplates
}
$searchableTemplate = Get-Item 'master:\templates\Foundation\Experience Accelerator\Search\Computed Fields\_Searchable'
# copy templates to project
$CalendarEventTemplate = Assert-FeatureTemplateInProject -TenantTemplateFolder $tenantTemplatesPath -FeatureTemplatePath "Feature/Experience Accelerator/Events/Datasource" -FeatureTemplateName "Calendar Event"
$EventListTemplate = Assert-FeatureTemplateInProject -TenantTemplateFolder $tenantTemplatesPath -FeatureTemplatePath "Feature/Experience Accelerator/Events/Datasource" -FeatureTemplateName "Event List"
$EventsFolderTemplate = Assert-FeatureTemplateInProject -TenantTemplateFolder $tenantTemplatesPath -FeatureTemplatePath "Feature/Experience Accelerator/Events/Datasource" -FeatureTemplateName "Events Folder"
# make events searchable
Add-BaseTemplate -Item $CalendarEventTemplate -TemplateItem $searchableTemplate
# set insert options
Add-ToStandardValues -Template $EventsFolderTemplate -InsertTemplates $EventListTemplate
Add-ToStandardValues -Template $EventListTemplate -InsertTemplates $CalendarEventTemplate
Get-ChildItem "$sitePath\Data" -Recurse | ? { $_.TemplateName -eq "Calendar Event" } | % { Set-ItemTemplate -Item $_ -TemplateItem $CalendarEventTemplate;}
Get-ChildItem "$sitePath\Data" -Recurse | ? { $_.TemplateName -eq "Event List" } | % { Set-ItemTemplate -Item $_ -TemplateItem $EventListTemplate; $_.__Masters = $CalendarEventTemplate }
Get-ChildItem "$sitePath\Data" -Recurse | ? { $_.TemplateName -eq "Events Folder" } | % { Set-ItemTemplate -Item $_ -TemplateItem $EventsFolderTemplate; $_.__Masters = $EventListTemplate }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.