Last active
February 27, 2020 10:54
-
-
Save feanz/aa578855babbc807e5bfc9c795376aef to your computer and use it in GitHub Desktop.
Copy sitecore site with a new lanaguage in sitecore powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Function CommonFunctions | |
$siteDropdownList = Get-Sites | |
$languageDropdownList = Get-Languages | |
$props = @{ | |
Parameters = @( | |
@{Name="sourceSitePath"; Title="Choose a source site"; Options=$siteDropdownList; Tooltip="Choose one."}, | |
@{Name="sourceLanguage"; Title="Choose a source language"; Options=$languageDropdownList; Tooltip="Choose one."}, | |
@{Name="destinationSiteName"; Title="Provide a destination site name"; Value=""; Tooltip="Choose a new site name."}, | |
@{Name="destinationLanguage"; Title="Choose a language for the destination site"; Options=$languageDropdownList; Tooltip="Choose one."}, | |
@{Name="includeSharedContent"; Title="Update shared content"; Value=$true; Tooltip="Select this to update shared content in the destination language and update references."} | |
) | |
Title = "Copy site" | |
Description = "This script will take a source site and copy it to create a new site, copying from the source language into the target one. All references to the source site in items will be updated to reference the new site." | |
Width = 500 | |
Height = 600 | |
} | |
$result = Read-Variable @props | |
if($result -ne "ok") | |
{ | |
Exit | |
} | |
Write-Host "COPY SITE V1.0" | |
Write-Host "==============================" | |
Write-Host "" | |
Write-Color "Variables:" -Color Magenta | |
Write-Host "" | |
Write-Color "Source path: ", $sourceSitePath -Color White, Yellow | |
Write-Color "Source lang: ", $sourceLanguage -Color White, Yellow | |
Write-Color "Target path: ", $destinationSiteName -Color White, Yellow | |
Write-Color "Target lang: ", $destinationLanguage -Color White, Yellow | |
Write-Color "Include shared: ", $includeSharedContent -Color White, Yellow | |
Write-Host "" | |
Write-Host "==============================" | |
Write-Host "" | |
## Step 1: Validate the new site name to confirm it doesn't exist | |
if ($siteDropdownList[$destinationSiteName]) { | |
Write-Host "Destination site already exists" | |
Show-Alert "Destination site $($destinationSiteName) already exists" | |
} | |
## Step 2: Copy the site to create the new site | |
$destinationSitePath = "/sitecore/content/$($destinationSiteName)" | |
Write-Color "Copying ", $sourceSitePath, " to ", $destinationSitePath -Color White, Yellow, White, Yellow | |
$sourceSiteItem = Get-Item master:$sourceSitePath | |
$sourceSiteItem | Get-ChildItem | ForEach-Object { | |
$name = $_.Name | |
if(![string]::IsNullOrEmpty($name)) | |
{ | |
$newName = "$($name)" # Add some logic here to rename the folder | |
$newPath = "$($destinationSitePath)/$($newName)" | |
Copy-Item -Path $_.ItemPath -Destination $newPath -Recurse | |
Write-Host “Item moved to: $($newPath)“ | |
} | |
else | |
{ | |
Write-Host “Couldn’t move Item: ” $name | |
} | |
} | |
## Step 3: Add new item versions in destination language | |
# Get all the items in the SOURCE language, as we need this to create the DESTINATION language | |
$itemsToProcess = @() | |
$itemsToProcess += Get-ChildItem -Path master:$destinationSitePath -WithParent -Recurse -Language $sourceLanguage | |
if ($includeSharedContent) { | |
$itemsToProcess += Get-ChildItem -Path master:/sitecore/content/Shared -Recurse -Language $sourceLanguage | |
} | |
Write-Color "Copying source language ", $sourceLanguage, " to ", $destinationLanguage -Color White, Yellow, White, Yellow | |
$itemsToProcess | Add-ItemVersion -Language $sourceLanguage -TargetLanguage $destinationLanguage -IfExist Append | |
## Step 4: Update references in content in the destination language | |
# Now we're operating on items in the DESTINATION language, so we'll reget all of the items. | |
$itemsToProcess = @() | |
$itemsToProcess += Get-ChildItem -Path master:$destinationSitePath -WithParent -Recurse -Language $destinationLanguage | |
if ($includeSharedContent) { | |
$itemsToProcess += Get-ChildItem -Path master:/sitecore/content/Shared -Recurse -Language $destinationLanguage | |
} | |
# Update the references | |
Write-Color "Updating item references from ", $sourceSitePath, " to ", $destinationSitePath -Color White, Yellow, White, Yellow | |
$itemsToProcess | Update-References -SourcePath $sourceSitePath -DestinationPath $destinationSitePath | |
# Step 5: Remove old language version | |
Write-Color "Remove old language version from ", $destinationSitePath -Color White, Yellow | |
# Get all items again in the source language | |
$itemsToProcess = @() | |
$itemsToProcess += Get-ChildItem -Path master:$destinationSitePath -WithParent -Recurse -Language $sourceLanguage | |
$itemsToProcess | Remove-ItemVersion -Version * -Language $sourceLanguage | |
Write-Color "Old language versions removed" -Color Yellow | |
Write-Host | |
Write-Color "Site copy complete!" -Color Yellow |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment