Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Import-Function New-Tenant
Import-Function Get-SiteItem
function Get-AffectedTenantTemplatesRoots {
[CmdletBinding()]
param()
begin {
Write-Verbose "Cmdlet Get-AffectedTenantTemplatesRoots - Begin"
}
process {
Write-Verbose "Cmdlet Get-AffectedTenantTemplatesRoots - Process"
$query = "/sitecore/content//*[contains(@#Templates#, '{')]"
$tenants = Get-Item -Path master: -Language en -Query $query
$tenantTemplateRoots = $tenants | ? { Test-ItemIsTenant $_ } | % { Get-Item master: -Id $_.Templates }
$affectedTenantTemplatesRoots = $tenantTemplateRoots | ? {
$root = $_
$tenantTemplates = gci $root.Paths.Path -r | ? { $_.TemplateName -eq "Template"}
$valid = $tenantTemplates | ? { $_."__Base Template".Contains(([Sitecore.XA.Foundation.Presentation.Templates+PartialDesigns]::ID.ToString())) }
if ($valid) {
write-host "This is valid $($_.Paths.path)"
$false
}
else {
$true
}
}
$affectedTenantTemplatesRoots
}
end {
Write-Verbose "Cmdlet Get-AffectedTenantTemplatesRoots - End"
}
}
function ShouldRun () {
$affectedTTR = Get-AffectedTenantTemplatesRoots
$affectedTTR.Count -gt 0
}
function Get-DialogParameters () {
}
function Test-ItemIsTenant {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0 )]
[Item]$Item
)
begin {
Write-Verbose "Cmdlet Test-ItemIsPartialDesign - Begin"
}
process {
Write-Verbose "Cmdlet Test-ItemIsPartialDesign - Process"
[Sitecore.Data.ID]$PartialDesignTemplateID = [Sitecore.XA.Foundation.Multisite.Templates+Tenant]::ID
[Sitecore.Data.Managers.TemplateManager]::GetTemplate($Item).InheritsFrom($PartialDesignTemplateID)
}
end {
Write-Verbose "Cmdlet Test-ItemIsPartialDesign - End"
}
}
function Get-PresentationItem {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0 )]
[Item]$CurrentItem
)
begin {
Write-Verbose "Cmdlet Get-PageDesignsItem - Begin"
}
process {
Write-Verbose "Cmdlet Get-PageDesignsItem - Process"
$instance = [Sitecore.XA.Foundation.IoC.ServiceLocator]::Current
$instance.GetType().GetMethod('Resolve').MakeGenericMethod([Sitecore.XA.Foundation.Presentation.IPresentationContext]).Invoke($instance, $null).GetPresentationItem($CurrentItem) | Wrap-Item
}
end {
Write-Verbose "Cmdlet Get-PageDesignsItem - End"
}
}
function Get-TenantTemplateOrCreate {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0 )]
[Item[]]$TenantTemplates,
[Parameter(Mandatory = $true, Position = 1 )]
[Sitecore.Data.ID]$TemplateId
)
begin {
Write-Verbose "Cmdlet Get-TenantTemplateOrCreate - Begin"
}
process {
Write-Verbose "Cmdlet Get-TenantTemplateOrCreate - Process"
$templateItem = Get-ProjectTemplateBasedOnBaseTemplate $TenantTemplates $TemplateId
if (-not $templateItem) {
Write-Verbose "Creating MetadataPartialDesign"
$templateItem = New-TenantTemplate $root $TemplateId
}
$templateItem
}
end {
Write-Verbose "Cmdlet Get-TenantTemplateOrCreate - End"
}
}
function Invoke-UpgradeStep {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true, Position = 0 )]
$arguments
)
process {
Write-Verbose "Getting affected tennat templates roots . . ."
$affectedTTR = Get-AffectedTenantTemplatesRoots
$result = New-Object Sitecore.XA.Foundation.Upgrade.Model.UpgradeStepResult
try {
$result.Result = "Success"
if ($affectedTTR.Count -gt 0) {
$affectedTTR | ForEach-Object {
$root = $_
if ($pscmdlet.ShouldProcess($root.Paths.Path, "Adding Sticky Notes base template to tenant templates")) {
$tenantTemplates = Get-ChildItem $root.Paths.Path -r | ? { $_.TemplateName -eq "Template"}
# Get templates for migration
$template_MetadataPartialDesign = Get-TenantTemplateOrCreate $tenantTemplates ([Sitecore.XA.Foundation.Presentation.Templates+MetadataPartialDesign]::ID.ToString())
$template_PartialDesign = Get-TenantTemplateOrCreate $tenantTemplates ([Sitecore.XA.Foundation.Presentation.Templates+PartialDesign]::ID.ToString())
$template_Design = Get-TenantTemplateOrCreate $tenantTemplates ([Sitecore.XA.Foundation.Presentation.Templates+Design]::ID.ToString())
$template_DesignFolder = Get-TenantTemplateOrCreate $tenantTemplates ([Sitecore.XA.Foundation.Presentation.Templates+DesignFolder]::ID.ToString())
$template_PartialDesigns = Get-TenantTemplateOrCreate $tenantTemplates ([Sitecore.XA.Foundation.Presentation.Templates+PartialDesigns]::ID.ToString())
$affectedTenantTemplates = ($template_PartialDesigns, $template_PartialDesign, $template_DesignFolder, $template_Design, $template_MetadataPartialDesign)
#Set templates insert options
$a1 = Get-Item master: -Id "{659FC30A-5979-401E-914D-52A9A2AC3E06}" # Partial Designs - Insert Options
$a2 = Get-Item master: -Id "{CF2A53E9-DF1D-4714-BDE1-A880CE4F7D86}" # Sticky Notes - Meta Partial Design
# $a3 = Get-Item master: -Id "{71B2AF6E-8D3B-4950-9303-5397E8775612}" # Sticky Notes - Page
$a4 = Get-Item master: -Id "{E014E623-0D90-4A6B-AC98-23604E032C42}" # Sticky Notes - Page Design
$a5 = Get-Item master: -Id "{D14C72EA-827D-4832-982F-93016983985B}" # Sticky Notes - Partial Design
Invoke-AddInsertOptionAdvanced $a1 $tenantTemplates
Invoke-AddBaseTemplate $a2 $tenantTemplates
Invoke-AddBaseTemplate $a4 $tenantTemplates
Invoke-AddBaseTemplate $a5 $tenantTemplates
Set-InsertOptionsForTenantTemplate $affectedTenantTemplates
# GetSiteSettingsItems
$template_Settings = $tenantTemplates | ? { $_."__Base Template".Contains("8517DBE7-68BC-4700-AD41-379C4EBB76A0") }
$query = "/sitecore/content//*[@@templateid='$($template_Settings.ID)']"
$settingItems = Get-Item -Path master: -Language en -Query $query
# Use PresentationContext to get presentationItems
# Use presentationItems as a roots for Set-TenantTemplate cmdlet
$presentationItems = $settingItems | % { Get-PresentationItem $_ }
$presentationItems | % {
if ($pscmdlet.ShouldProcess($_.Parent.Paths.Path, "Switching tenant templates of existing site's items")) {
Set-TenantTemplate $_ $affectedTenantTemplates
}
}
}
}
}
}
catch {
$result.Result = "Error"
$result.Message = ""
$result.ErrorObject = $Error[0]
}
finally {
$result
}
}
}
Invoke-UpgradeStep ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment