Skip to content

Instantly share code, notes, and snippets.

@fluxdigital
Last active July 6, 2022 15:38
Show Gist options
  • Save fluxdigital/e5d2f98d7de1cee3756613cc097f1294 to your computer and use it in GitHub Desktop.
Save fluxdigital/e5d2f98d7de1cee3756613cc097f1294 to your computer and use it in GitHub Desktop.
<#
### Creates Draft Sites for all existing Sites - for use on the CM Instance ###
.Description
For each site -> Duplicates the existing Site Grouping item and adds 'DRAFT' to the name.
Sets the environment to 'Draft' and database to 'master' also sets the target hostname to the current CM instance Url.
#>
$rootItem = Get-Item -Path "/sitecore/content"
$sxaSites = Get-ChildItem -Path "/sitecore/content/Sites"
$successCount = 0
$failCount = 0
$siteGroupingPath = "Settings/Site Grouping"
$previewSiteName = "DRAFT"
$previewSiteEnvName = "Draft"
$global:limitCount = 0
$maxLimitCount = 0 #for limiting to x number of sites before exiting. 0 means don't limit/all sites.
function Get-CmUrl($item){
$site = [Sitecore.Sites.SiteContext]::GetSite("website")
New-UsingBlock(New-Object -TypeName "Sitecore.Sites.SiteContextSwitcher" -ArgumentList $site) {
$urlOptions = [Sitecore.Links.LinkManager]::GetDefaultUrlOptions()
$urlOptions.AlwaysIncludeServerUrl = $True
$urlOptions.ShortenUrls = $True
$urlOptions.SiteResolving = $True;
$url = [Sitecore.Links.LinkManager]::GetItemUrl($item, $urlOptions)
return $url.TrimEnd('/')
}
}
$cmHostname = Get-CmUrl -Item $rootItem
write-host "CM Hostname: $($cmHostname)"
foreach ($sxaSite in $sxaSites) {
try
{
write-host "Adding Site Grouping for: $($sxaSite.Name)..."
if(($global:limitCount -le $maxLimitCount) -or ($maxLimitCount -eq 0)){
$siteItemPath = "$($sxaSite.Paths.FullPath)/$($siteGroupingPath)/$($sxaSite.Name)"
$previewSiteItemPath = "$($sxaSite.Paths.FullPath)/$($siteGroupingPath)/$($sxaSite.Name) $($previewSiteName)"
$siteItem = Get-Item -Path $siteItemPath -ErrorAction SilentlyContinue #Get the sitecore Item
$previewSiteItem = Get-Item -Path $previewSiteItemPath -ErrorAction SilentlyContinue #Get the sitecore Item
write-host "checking site path: $($siteItemPath)..."
if($siteItem -ne $null) #Check Site exists
{
write-host "found site: $($siteItemPath)" -ForegroundColor gray
}
write-host "checking preview site path: $($previewSiteItemPath)..."
if($previewSiteItem -ne $null) #Check if preview Site exists
{
write-host "already exists: $($previewSiteItemPath) - skipping" -ForegroundColor yellow
}
else{
try
{
#dupe the site item
copy-Item -Path "master:$($siteItemPath)" -Destination "master:$($previewSiteItemPath)"
write-host "Preview Site Item created: $($previewSiteItem.Name)"
$successCount+=1
$global:limitCount+=1
}
catch
{
write-host "Failed to create Preview Site Item: master:$($previewSiteItemPath)" -ForegroundColor red
write-host $_.Exception.Message
$failCount +=1
continue
}
}
$createdPreviewItem = Get-Item -Path $previewSiteItemPath -ErrorAction SilentlyContinue
if($createdPreviewItem){
#set field values
$createdPreviewItem.Editing.BeginEdit()
$createdPreviewItem["SiteName"] = "$($sxaSite.Name) $($previewSiteName)"
$createdPreviewItem["Environment"] = "$($previewSiteEnvName)"
$createdPreviewItem["Database"] = "master"
$createdPreviewItem["TargetHostName"] = $cmHostname
$createdPreviewItem.Editing.EndEdit()
}
else{
write-host "couldnt find Preview Site Item: " $previewSiteItemPath -ForegroundColor red
}
}
}
catch
{
write-host "Error with Site item" -ForegroundColor red
write-host $_.Exception.Message
$failCount +=1
continue
}
}
write-host "--- complete - successful: $($successCount) failed: $($failCount)---"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment