Skip to content

Instantly share code, notes, and snippets.

@peplau
Last active November 10, 2022 20:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peplau/ad9ff6daea9593ae376a70789be478a1 to your computer and use it in GitHub Desktop.
Save peplau/ad9ff6daea9593ae376a70789be478a1 to your computer and use it in GitHub Desktop.
SPE: Advanced Quick Download Tree as Package - Allows the user to pick and choose among Linked Items to include in the package as well
Import-Function -Name Setup-PackageGeneratorAdvanced
$timestamp = Get-Date -Format "yyyyMMdd.HHss"
$selectedItem = Get-Item -Path .
$examplePackageName = "$($timestamp).$($selectedItem.Name)"
$includeLinkedItems = 0
$parameters = @(
@{ Name = "packageName"; Title="Package Name"; Placeholder = $examplePackageName; Tab="Package Metadata"},
@{ Name = "Author"; Value = [Sitecore.Context]::User.Profile.FullName; Tab="Package Metadata"},
@{ Name = "Publisher"; Value = [Sitecore.SecurityModel.License.License]::Licensee; Tab="Package Metadata"},
@{ Name = "Version"; Value = $selectedItem.Version; Tab="Package Metadata"},
@{ Name = "Readme"; Title="Readme"; Lines=10; Tab="Package Metadata"},
@{ Name = "AsXml"; Title="Download Package as XML"; Value=[bool]$False; Editor="bool"; Tab="Package Metadata" },
@{ Name = "IncludeItems"; Title="Items to include in package"; Value="RootAndDescendants"; Options=$rootOptions; OptionTooltips = $rootOptionTooltips; Tooltip = "Hover over each option to view a short description."; Hint = "The package will dynamically include the items based on your selection below. <br /><br />Root : '$($selectedItem.ProviderPath)'"; Editor="combo"; Tab="Installation Options" },
@{ Name = "Mode"; Title="Installation Options"; Value = "Merge-Merge"; Options = $installOptions; OptionTooltips = $installOptionsTooltips; Tooltip = "Hover over each option to view a short description."; Hint = "How should the installer behave if the package contains items that already exist?"; Editor="combo"; Tab="Installation Options"}
)
#if($selectedItem.ItemPath.StartsWith("/sitecore/content")) {
$parameters += @{ Name = "IncludeLinkOptions"; Title = "Include linked items"; Value=0; Tooltip = "Define how linked items will be included in the package"; Options = $linkOptions; OptionTooltips = $linkOptionsTooltips; Editor = "radio"; Tab="Linked Items" }
#}
$props = @{} + $defaultProps
$props["Title"] = "Download Tree as Package"
$props["Description"] = "This Tool allows you to download the item tree as a package quickly."
$props["Parameters"] = $parameters
$props["Width"] = 600
$props["Height"] = 750
$result = Read-Variable @props
Resolve-Error
if($result -ne "ok") {
Close-Window
Exit
}
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Undefined
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Undefined
switch ($Mode) {
"Overwrite" {
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Overwrite
}
"Merge-Merge" {
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Merge
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Merge
}
"Merge-Clear" {
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Merge
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Clear
}
"Merge-Append" {
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Merge
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Append
}
"Skip" {
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Skip
}
"SideBySide" {
$InstallMode = [Sitecore.Install.Utils.InstallMode]::SideBySide
}
"AskUser" {
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Undefined
}
}
# Linked items dialog
$linkedItems = @()
$itemsToPack = @()
$itemsToPack += Get-ChildrenToInclude $selectedItem $IncludeItems
# 1 - Only parent references are added to the package
if ($IncludeLinkOptions -eq 1){
$parentItem = $itemsToPack | Where-Object -FilterScript { $_.ID -like $selectedItem.ID }
if ($parentItem){
$linkedItems = Get-UniqueLinks $parentItem
}
}
# 2 - All references are added to the package
elseif ($IncludeLinkOptions -eq 2){
foreach ($item in $itemsToPack) {
$linkedItems += Get-UniqueLinks $item
}
}
if ($linkedItems.Count -gt 0){
$parameters = @()
$selectedLinks = @()
$linkedItems = $linkedItems|Sort-Object {$_.Paths.Path}
# Separate items by type
$covered = @()
$content = Filter-ByPathContains $linkedItems -Path "/sitecore/content/*"
if ($content -ne $null){
$covered += $content
}
$media = Filter-ByPathContains $linkedItems -Path "/sitecore/media library/*"
if ($media -ne $null){
$covered += $media
}
$layouts = Filter-ByPathContains $linkedItems -Path "/sitecore/layout/*"
if ($layouts -ne $null){
$covered += $layouts
}
$templates = Filter-ByPathContains $linkedItems -Path "/sitecore/templates/*"
if ($templates -ne $null){
$covered += $templates
}
$system = Filter-ByPathContains $linkedItems -Path "/sitecore/system/*"
if ($system -ne $null){
$covered += $system
}
$others = Get-OtherItems $covered $linkedItems
foreach ($template in $templates) {
$templateChildren = $template.Axes.GetDescendants()
$templates += $templateChildren
$linkedItems += $templateChildren
}
$templates = $templates|Sort-Object {$_.Paths.Path}
$linkedItems = $linkedItems|Sort-Object {$_.Paths.Path}
# Build dialogs for each type
if ($content){
$options = Get-OptionsForItems $content $selectedItem
$parameters += @{ Name = "IncludedContent"; Title = "Content"; Options = $options; Editor = "checkbox"; Tab="Content ("+$options.Count+")" }
}
if ($media){
$options = Get-OptionsForItems $media $selectedItem
$parameters += @{ Name = "IncludedMedia"; Title = "Media"; Options = $options; Editor = "checkbox"; Tab="Media ("+$options.Count+")" }
}
if ($layouts){
$options = Get-OptionsForItems $layouts $selectedItem
$parameters += @{ Name = "IncludedLayouts"; Title = "Layouts"; Options = $options; Editor = "checkbox"; Tab="Layouts ("+$options.Count+")" }
}
if ($system){
$options = Get-OptionsForItems $system $selectedItem
$parameters += @{ Name = "IncludedSystem"; Title = "System"; Options = $options; Editor = "checkbox"; Tab="System ("+$options.Count+")" }
}
if ($templates){
$options = Get-OptionsForItems $templates $selectedItem
$parameters += @{ Name = "IncludedTemplates"; Title = "Templates"; Options = $options; Editor = "checkbox"; Tab="Templates ("+$options.Count+")" }
}
if ($others){
$options = Get-OptionsForItems $others $selectedItem
$parameters += @{ Name = "IncludedOthers"; Title = "Others"; Options = $options; Editor = "checkbox"; Tab="Others ("+$options.Count+")" }
}
$props = @{} + $defaultProps
$props["Title"] = "Download Tree as Package - Select Linked Items"
$props["Description"] = "Choose the Linked Items that you want to include in the package."
$props["Parameters"] = $parameters
$props["Width"] = 800
$props["Height"] = 750
$result = Read-Variable @props
Resolve-Error
if($result -ne "ok") {
Close-Window
Exit
}
$selectedLinks = $IncludedContent + $IncludedMedia + $IncludedLayouts + $IncludedSystem + $IncludedTemplates + $IncludedOthers
}
# Add selected linked items to package
if ($selectedLinks -AND $selectedLinks.Count -gt 0){
foreach ($linkId in $selectedLinks) {
$itemsToPack += $linkedItems | Where-Object -FilterScript { $_.ID.ToString() -like $linkId }
}
}
# Adding items to package
foreach ($itemToPack in $itemsToPack) {
$package = Add-ItemToPackage -Package $package -Item $itemToPack -IncludeDescendants $false
}
if([string]::IsNullOrEmpty($packageName)) {
$packageName = $examplePackageName
}
$package.Name = $packageName
$package.Metadata.Author = $Author
$package.Metadata.Publisher = $Publisher
$package.Metadata.Version = $Version
$package.Metadata.Readme = $Readme
[string]$packageName = "$($package.Name)-$($package.Metadata.Version)".Trim('-')
if ($AsXml) {
$packageFileName = "$($packageName).xml"
}
else {
$packageFileName = "$($packageName).zip"
}
Export-Package -Project $package -Path $packageFileName -Zip:$(!$AsXml)
Download-File "$($SitecorePackageFolder)\$($packageFileName)"
Remove-Item "$($SitecorePackageFolder)\$($packageFileName)"
Close-Window
Import-Function -Name Setup-PackageGenerator
function Get-UniqueLinks {
param(
[item]$Item
)
$combined = @()
$combined += Get-UniqueReferrers $Item
$combined += Get-UniqueReferences $Item
return $combined | Sort-Object -Property @{Expression={$_.ID}} -Unique
}
function Get-UniqueReferrers {
param(
[item]$Item
)
$items = Get-ItemReferrer -Item $Item
return $items | Sort-Object -Property @{Expression={$_.ID}} -Unique
}
function Get-UniqueReferences {
param(
[item]$Item
)
$links = $Item.Links.GetAllLinks($true,$true)
$items = @()
foreach ($link in $links) {
$path = $link.TargetDatabaseName
$path += ":" + $link.TargetPath
try {
$item = Get-Item -Database $link.TargetDatabaseName -Path $path -ErrorAction SilentlyContinue
if ($item -ne $null){
$items += $item
}
}
catch {}
}
return $items | Sort-Object -Property @{Expression={$_.ID}} -Unique
}
function Get-OptionsForItems {
param(
[item[]]$Items,
[Item]$baseItem
)
$options = [ordered]@{}
foreach ($item in $Items) {
$display = $item.Paths.Path.ToString()
if ($baseItem.Database.Name -ne $item.Database.Name){
$display = $item.Database.Name + ":" + $display
}
if ($options.Contains($display) -eq $false){
$options.Add($display, $item.ID.ToString())
}
}
return $options
}
function Filter-ByPathContains {
param(
[item[]]$Items,
[string]$path
)
$filtered = @()
$filtered += $Items | Where-Object -FilterScript { $_.Paths.Path -like $path }
return $filtered
}
function Get-OtherItems {
param(
[item[]]$coveredItems,
[item[]]$allItems
)
$others = @()
foreach ($i in $allItems) {
$filtered = $coveredItems | Where-Object -FilterScript { $_.ID -eq $i.ID }
if ($filtered -eq $null -or $filtered.Count -eq 0){
$others += $i
}
}
return $others
}
function Get-ChildrenToInclude {
param(
[Item]$baseItem,
[string]$rootOption
)
$items = @()
if($rootOption -eq "RootAndDescendants"){
$items += $baseItem
$items += $baseItem.Axes.GetDescendants()
}
elseif($rootOption -eq "RootAndChildren"){
$items += $baseItem
$items += $baseItem.Children
}
elseif($rootOption -eq "DescendantsOnly"){
$items += $baseItem.Axes.GetDescendants()
}
elseif($rootOption -eq "ChildrenOnly"){
$items += $baseItem.Children
}
return $items
}
$linkOptions = [ordered]@{
"Do not include linked items" = 0
"Include parent links only" = 1
"Include all linked items" = 2
}
$linkOptionsTooltips = [ordered]@{
0 = "Linked items are not included in the package"
1 = "Only items linked to the parent will be included in the package"
2 = "All linked items will be included in the package"
}
Line 1 - Changed the Import-Function Name to Setup-PackageGeneratorAdvanced
Line 5 - Removed as the $path variable was not used
Lines 20-22 - Updated to always show the Links tab, which is now a radiobox
Line 74 - Replaced by the block from lines 75 to 149
Lines 76 to 87 - Replaced by the block from lines 151 to 162
@michaellwest
Copy link

Cool. I'll give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment