Sitecore Powershell - Creating a new sitecore multisite, Update Base Template, Renderings,Templates,Dictionary and Content
I had a requirement to create a brand-new
site with same content structure (later content will be updated as per new
site) based on the existing Sitecore site by duplicating all the templates
and rendering and to host the site in the same Sitecore instance, Duplicating
the site is very easy and hosting also done very easily but the problem is all
the layout and Rendering is pointing to the old site from where we duplicated. Also,
while duplicating the templates its inherited templates will still point to the
old templates.
So,
this is the big problem as if any changes happen to the old site's templates
will reflect in the new site, but we need separate templates/Rendering/Layouts
for the new site.
So,
I thought of achieving with Sitecore Powershell scripts.
Let's
do it.
Step 1:(Rendering Updates to new
site's View and Controller)
$allItems
= Get-ChildItem -Path 'master:///sitecore/layout/Renderings/Projects/NewSite'
-Recurse
$filteredItems
=$allItems| Where-Object { $_.TemplateName -eq 'View rendering' }
ForEach
($item in $allItems)
{
#Replacing
the View path pointing to OldSite Views
if($item.TemplateName -eq 'View rendering')
{
$item.Editing.BeginEdit()
$item["Path"] =
$item.Fields["Path"].toString().replace("OldSite",
"NewSite");
$item.Editing.EndEdit();
Write-Host
$item["Path"] ;
}
#Replacing
the Controller pointing to OldSite
if($item.TemplateName -eq 'Controller
rendering')
{
$item.Editing.BeginEdit()
$item["Controller"]
= $item.Fields["Controller"].toString().replace("OldSite",
"NewSite");
$item.Editing.EndEdit();
Write-Host $item["Controller"] ;
}
}
Step 2:(Update the Base Template
and remove old site’s pointing base template)
$templateFilter
= Get-Item
"master:/sitecore/templates/System/Templates/Template";
$rootTemplate
= Get-ChildItem 'master:/sitecore/templates/Projects/NewSite'
-Recurse | Where-Object { $_.TemplateName -eq $templateFilter.Name
} ;
#Looping
through the templates
ForEach
($templates in $rootTemplate) {
Write-Host
"Template name" $templates.Name
$basetemplates= Get-Item -Path master: -ID $templates."__Base
template"
#Looping through base templates in the template
ForEach ($basetemplate in $basetemplates)
{
Write-Host $basetemplate.Paths.FullPath
if($basetemplate.Name -ne "Standard template")
{
if($basetemplate.Paths.FullPath.Contains("OldSite"))
{
if($basetemplate -ne $null)
{
#Add base
templates of NewSite for the same template
Add-BaseTemplate
-Path $children.Paths.FullPath -Template $basetemplate.Paths.FullPath().replace("OldSite",
"NewSite")
#remove base
templates of Oldsite in the New sites template
Remove-BaseTemplate -Path $children.Paths.FullPath -Template
$basetemplate.Paths.FullPath
}
}
}
}
}
Step 3: (Add New Sites Rendering and remove Oldsite Rendering in content
Items)
$allItems = Get-ChildItem -Path
'master:///sitecore/content/NewSite' -Recurse
ForEach ($item in $allItems) {
#Check for Presentation details
if (Get-Layout $item)
{
$AllItemRenderings = Get-Rendering -Item $item -Device $defaultLayout
-FinalLayout:$useFinalLayout
foreach ( $rendering in
$AllItemRenderings )
{
$itemRendering= Get-Item $rendering.ItemID
$pathofNewRendering=$itemRendering.Paths.FullPath.replace("OldSite","NewSite");
#Get New Rendering item
$NewRenderingItem= Get-Item
$pathofNewRendering
Write-Host "Placholder
:" $rendering.Placeholder
:"Rendering Name : " $itemRendering.Name "/ "
$NewRenderingItem.Name "ItemName:" $item.Name "Item Path :"
$item.Paths.FullPath
if($rendering.Placeholder)
{
$renderingToAdd = gi
master:$pathofNewRendering | New-Rendering -Placeholder $rendering.Placeholder
Add-Rendering -Item $item -Rendering $renderingToAdd
-Placeholder $rendering.Placeholder
$renderingToRemove =
Get-Item -Path master: -ID $rendering.ItemID
Remove-Rendering -Item $item
-Rendering $renderingToRemove
}
else
{
#Some times placeholder
directly added to the renderings itself without in presentation details of items
#$itemRendering["Placeholder"] getting placeholder value
$renderingToAdd = gi
master:$pathofNewRendering | New-Rendering -Placeholder
$itemRendering["Placeholder"]
Add-Rendering -Item $item -Rendering $renderingToAdd
-Placeholder $itemRendering["Placeholder"]
$renderingToRemove =
Get-Item -Path master: -ID $rendering.ItemID
Remove-Rendering -Item
$item -Rendering $renderingToRemove
Write-Host "Placholder
:" $aItems["Placeholder"]
:"Rendering Name : " $itemRendering.Name "/ "
$NewRenderingItem.Name "ItemName:" $item.Name "Item Path :"
$item.Paths.FullPath
}
}
}
}
Steps 4: (Templates changing for
all content Items from OldSite to NewSite)
$allItems = Get-ChildItem -Path
'master:///sitecore/content/NewSite -Recurse
ForEach ($item in $allItems) {
$sourceTemplate = Get-Item $item.TemplateID
$targetTemplate = Get-Item $sourceTemplate.Paths.FullPath.replace("OldSite","NewSite")
$item.ChangeTemplate($targetTemplate);
Write-Host "ItemName :" $item.Name "SourceTemplate:"
$sourceTemplate.Paths.FullPath "TargetTemplate:" $targetTemplate.Paths.FullPath
}
Step 5: (Update the Dictionary
Key from Oldsite to New Site)
$dictionaryItems = Get-ChildItem
-Path 'master:///sitecore/content/System/Dictionary/NewSite' -Recurse
$filteredItems =$dictionaryItems|
Where-Object { $_.TemplateName -eq 'Dictionary entry' }
ForEach ($item in $filteredItems)
{
if($item["Key"].StartsWith("OldSite")
)
{
$item.Editing.BeginEdit()
$item["Key"] =
$item.Fields["Key"].toString().replace("OldSite",
"NewSite");
$item.Editing.EndEdit();
Write-Host
$item["Phrase"] + "-------------"+
$item["Key"].toString();
}
}
Now we have updated all the
contents and Rendering for the new site.
That’s it 😊
Comments
Post a Comment