Last active
March 10, 2021 11:18
-
-
Save vtml/978c5c817b7c74ec91820bd6a8def433 to your computer and use it in GitHub Desktop.
Sitecore Creative Exchange Import SPE script
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
<# | |
.SYNOPSIS | |
Performs a Sitecore Creative Exchange Import from the files on disk | |
.DESCRIPTION | |
This script utilises the Sitecore Creative Exchange to import front end static assets into the media library. This gives the front end development team full flexibility to use the tools | |
they are needed to produce the final output. This is a Sitecore Powershell Extension script that should be stored where all the other Script Libraries are placed in Sitecore. | |
.PARAMETER scriptArguments.DeviceId | |
The Device ID. If left empty, it will use the Sitecore Default Device. Please note that a Full Sitecore ID with curly braces are expected. | |
.PARAMETER scriptArguments.SitePath | |
The full Sitecore Item path to the Site in SXA. eg. /sitecore/content/Habitat SXA Sites/Habitat Home | |
.INPUTS | |
None. You cannot pipe objects to this script. | |
.OUTPUTS | |
None. This script performs the Creative Exchange Import | |
.EXAMPLE | |
N/A. This Sitecore Powershell Extensions script is triggered using Sitecore Powershell Extensions Restful API v2. | |
#> | |
Import-Function Get-CreativeExchangeImportResponse | |
if ($scriptArguments.ContainsKey("SitePath")) | |
{ | |
if (-Not $scriptArguments.ContainsKey("DeviceId")) | |
{ | |
# Defaults to the Default Device in Sitecore | |
$deviceId = "{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}" | |
} | |
else { | |
$deviceId = $scriptArguments.DeviceId | |
} | |
$database = Get-Database -Name "master" | |
$siteItem = Get-Item -Path $scriptArguments.SitePath | |
$themesFolder = $siteItem.ThemesFolder | |
$pageDesignItem = Get-Item -Path "$($siteItem.ItemPath)/Presentation/Page Designs" | |
$globalScriptsOptimizerItem = Get-Item -Path "master:/system/Settings/Foundation/Experience Accelerator/Theming/Optimiser/Scripts" | |
$globalStylesOptimizerItem = Get-Item -Path "master:/system/Settings/Foundation/Experience Accelerator/Theming/Optimiser/Styles" | |
if (-Not [string]::IsNullOrEmpty($themesFolder) -and $pageDesignItem -ne $null -and $globalScriptsOptimizerItem -ne $null -and $globalStylesOptimizerItem -ne $null) | |
{ | |
$themesFolderItem = Get-Item -Path "master:" -ID $themesFolder | |
# Check to see if asset optimizer is used for the site | |
$globalStylesOptimizerEnum = Get-Item -Path master: -ID $globalStylesOptimizerItem.Fields["Mode"].Value | |
$globalScriptsOptimizerEnum = Get-Item -Path master: -ID $globalScriptsOptimizerItem.Fields["Mode"].Value | |
$globalStylesOptimizerValue = $globalStylesOptimizerEnum.Fields["Value"].Value | |
$globalScriptsOptimizerValue = $globalScriptsOptimizerEnum.Fields["Value"].Value | |
$isStylesOptimizingEnabled = $pageDesignItem.Fields["Styles Optimizing Enabled"].Value | |
$isScriptsOptimizingEnabled = $pageDesignItem.Fields["Scripts Optimizing Enabled"].Value | |
if ($globalStylesOptimizerValue -eq "Disabled") | |
{ | |
$isStylesOptimizingEnabled = "0" | |
} | |
elseif ([string]::IsNullOrEmpty($isStylesOptimizingEnabled)) | |
{ | |
$isStylesOptimizingEnabled = "1" | |
} | |
if ($globalScriptsOptimizerValue -eq "Disabled") | |
{ | |
$isScriptsOptimizingEnabled = "0" | |
} | |
elseif ([string]::IsNullOrEmpty($isScriptsOptimizingEnabled )) | |
{ | |
$isScriptsOptimizingEnabled = "1" | |
} | |
Write-Host "Begin: Cleaning up theme folders..." | |
$ScriptsFolders = Get-ChildItem -ID $themesFolder -Recurse | Where-Object { $_.Name -eq 'Scripts' } | Get-ChildItem -ID $_.ID -Recurse | Where-Object { (($isScriptsOptimizingEnabled -eq "1" -and -not $_.Name.StartsWith("optimized")) -or ($isScriptsOptimizingEnabled -ne "1")) } | Remove-Item -Confirm:$false -Force -Recurse | |
$StylesFolders = Get-ChildItem -ID $themesFolder -Recurse | Where-Object { $_.Name -eq 'styles' } | Get-ChildItem -ID $_.ID -Recurse | Where-Object { (($isStylesOptimizingEnabled -eq "1" -and -not $_.Name.StartsWith("optimized")) -or ($isStylesOptimizingEnabled -ne "1")) } | Remove-Item -Confirm:$false -Force -Recurse | |
$FontsFolders = Get-ChildItem -ID $themesFolder -Recurse | Where-Object { $_.Name -eq 'fonts' } | Get-ChildItem -ID $_.ID -Recurse | Remove-Item -Confirm:$false -Force -Recurse | |
$imagesFolders = Get-ChildItem -ID $themesFolder -Recurse | Where-Object { $_.Name -eq 'images' } | Get-ChildItem -ID $_.ID -Recurse | Remove-Item -Confirm:$false -Force -Recurse | |
Write-Host "End: Cleaning up theme folders..." | |
Write-Host "Begin: Import Creative Exchange into Sitecore" | |
$homeItem = Get-Item -Path "$($siteItem.ItemPath)/Home" | |
$importArgs = Get-CreativeExchangeImportResponse -CurrentItem $homeItem -DeviceId $deviceId | |
$responseObject = @{ | |
"Messages" = $importArgs.Messages | |
} | |
Set-HostProperty -HostWidth 9999 | |
[Newtonsoft.Json.JsonConvert]::SerializeObject($responseObject) | |
Write-Host "End: Import Creative Exchange into Sitecore" | |
Write-Host "Begin: Publish all front end assets" | |
$targets = @() | |
foreach($publishingTarget in [Sitecore.Publishing.PublishManager]::GetPublishingTargets($database)) { | |
$targets += Get-Database -Name $publishingTarget[[Sitecore.FieldIDs]::PublishingTargetDatabase] | |
} | |
$languages = [Sitecore.Data.Managers.LanguageManager]::GetLanguages($database) | |
# Item publish with children | |
[Sitecore.Publishing.PublishManager]::PublishItem($themesFolderItem,$targets,$languages,$true,$true,$true) | |
Write-Host "End: Publish all front end assets" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment