SPE Starter Kit: Create Sitecore Package

Author

Brandon Bruno

Published

December 01, 2020

Tags

SPE Starter Kit: Create Sitecore Package

While developing a Sitecore solution, serialization plays an important role in sharing templates, settings, definition items, and sometimes even content between environments. Sometimes serialization is overkill, especially for quick-and-dirty jobs that just require a few specific types of items to be shared... or backed up... or restored from another environment. Sitecore packages can sometimes be just as quick, although the Package Designer in Sitecore can be cumbersome sometimes.

If you're a Sitecore developer that has needed to create a content package in a hurry, Sitecore PowerShell Extensions can solve the issue quickly with code. I've added a utility script to my SPE Starter Kit repo on GitHub.

The script:

# Load items to include in the package; use Where-Object to filter items
# A couple of Where-Object clauses are provided below

$items = Get-ChildItem -Path 'master:/sitecore/content' -Recurse
# | Where-Object { $_.TemplateName -eq 'TEMPLATE_NAME' }
# | Where-Object { $_.TemplateId -eq '{guid}' }

Write-Host "Found $($items.Count) items for package."

# Create (and configure) package and related metadata
$package = New-Package -Name 'PACKAGE_NAME'
$package.Metadata.Author = 'PACKAGE_AUTHOR'
$package.Metadata.Version = '1'
$package.Metadata.Readme = 'README_TEXT'

# Create a source for the items
$source = $items | New-ExplicitItemSource -Name 'PACKAGE_SOURCE_NAME' -InstallMode Overwrite
$package.Sources.Add($source)

# Create, export, and download the package
Export-Package -Project $package -Path "$($package.Name).zip" -Zip
Download-File "$SitecorePackageFolder\$($package.Name).zip"

This script let's a developer load items from the content tree (for example, with Find-Item or Get-ChildItem) and simply dumps out a Sitecore package ready for download. The script can be easily modified to create multiple packages.

Get it here: SPE Starter Kit: Utility - Create Sitecore Package

The official SPE documentation has all package management cmdlets covered: Packaging - Sitecore PowerShell Extensions

Do you have questions, comments, or corrections for this post? Find me on Twitter: @BrandonMBruno