Last active
April 19, 2018 08:38
-
-
Save Wesley-Lomax/edd96548f99f947dc8aec63cea365b34 to your computer and use it in GitHub Desktop.
Creates a Sitecore package for an item include the item, its templates and base templates, renderings and their datasource items and templates.
This file contains 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
function AddItemToCollection($itemId) | |
{ | |
$itemCollection.Add($itemId) | |
} | |
function GetRenderings() | |
{ | |
Get-Rendering -Item $startItem -FinalLayout | Foreach-Object { | |
if($_.ItemID) | |
{ | |
AddItemToCollection($_.ItemID) | |
} | |
if($_.DataSource) | |
{ | |
AddItemToCollection($_.DataSource) | |
} | |
} | |
Get-Rendering -Item $startItem | Foreach-Object { | |
if($_.ItemID) | |
{ | |
AddItemToCollection($_.ItemID) | |
} | |
if($_.DataSource) | |
{ | |
AddItemToCollection($_.DataSource) | |
} | |
} | |
} | |
$itemCollection = New-Object System.Collections.Generic.List[System.Object] | |
$startItem = Get-Item -Path "master:\sitecore\content\Home" -Language en | |
AddItemToCollection($startItem.ID) | |
GetRenderings | |
Write-Host "Total Items Found " $itemCollection.Count | |
$sourceItems = New-Object System.Collections.Generic.List[Sitecore.Data.Items.Item] | |
foreach($id in $itemCollection) | |
{ | |
$itemToAdd = Get-Item master -ID $id | |
if($itemToAdd -ne $null) | |
{ | |
$templateToAdd = Get-ItemTemplate -Item $itemToAdd | |
try{ | |
$sourceItems.Add($itemToAdd); | |
If ($templateToAdd.FullName -NotMatch "System") | |
{ | |
$sourceItems.Add($templateToAdd); | |
} | |
} | |
catch{ | |
} | |
$baseTemplates = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($templateToAdd).GetBaseTemplates(); | |
foreach($basetemplate in $baseTemplates) | |
{ | |
$baseTemplteItem = Get-Item master -ID $basetemplate.ID | |
If ($baseTemplteItem.ItemPath -NotMatch "System") | |
{ | |
try{ | |
$sourceItems.Add($baseTemplteItem); | |
} | |
catch{ | |
} | |
} | |
} | |
} | |
} | |
if($itemCollection.Count -gt 0) | |
{ | |
# Create package | |
$package = new-package ([string]::Format("Items Extract for {0}", $startItem.Name)); | |
# Set package metadata | |
$package.Sources.Clear(); | |
$package.Metadata.Publisher = "Principle Logic"; | |
$package.Metadata.Version = "1.0"; | |
$package.Metadata.Author = [Sitecore.Context]::User.Profile.FullName; | |
$package.Metadata.Readme = 'Package contains the item referenced, its renderings and datasource items, templates and base templates' | |
# Add content | |
$sourceItems = $sourceItems | sort -unique | |
$source =$sourceItems | New-ExplicitItemSource -Name "ItemsSource" -InstallMode Skip #-InstallMode Merge -MergeMode Merge | |
foreach($item in $sourceItems) | |
{ | |
try{ | |
$source.ExplicitItemSource.Add($item); | |
} | |
catch{} | |
} | |
$package.Sources.Add($source); | |
# Save package | |
Export-Package -Project $package -Path "$($package.Name)-$($package.Metadata.Version).zip" -Zip | |
# Offer the user to download the package | |
Download-File "$SitecorePackageFolder\$($package.Name)-$($package.Metadata.Version).zip" | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment