Skip to content

Instantly share code, notes, and snippets.

@AlexKasaku
Last active August 29, 2015 14:27
Show Gist options
  • Save AlexKasaku/5299e7e6fa0d0d9a0030 to your computer and use it in GitHub Desktop.
Save AlexKasaku/5299e7e6fa0d0d9a0030 to your computer and use it in GitHub Desktop.
Serialize all child-items along with their related media items
<#
.SYNOPSIS
Serialize items AND related media items.
.NOTES
Alex Washtell
#>
filter Is-InMediaLibrary {
if($_.FullPath.StartsWith("/sitecore/media library")) {
$_
}
}
$sourceItems = Get-ChildItem -recurse
$linkDb = [Sitecore.Globals]::LinkDatabase
$requiredAncestors = new-object 'System.Collections.Generic.HashSet[string]'
foreach($sourceItem in $sourceItems)
{
# Serialize item
Write-Host "Serializing $($sourceItem.Paths.FullPath)"
$sourceItem | serialize-item
# Find related media
$targetMediaItems = $linkDb.GetReferences($sourceItem) | %{ get-item master: -ID $_.TargetItemID } | Is-InMediaLibrary
foreach($targetItem in $targetMediaItems )
{
Write-Host "--- Related Media $($targetItem.Paths.FullPath)"
$targetItem | serialize-item
$parentItem = $targetItem.Parent
while ($parentItem -ne $null)
{
Write-Host "----- Adding parent $($parentItem.Paths.FullPath)"
$requiredAncestors.Add($parentItem.Paths.FullPath) > $null
$parentItem = $parentItem.Parent
}
}
}
# Process the set of required ancestors
foreach($ancestor in $requiredAncestors)
{
Write-Host "Required ancestor $($ancestor)"
serialize-item -Path $ancestor
}
Write-Host "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment