Automate Sitecore Packaging

Lately, I've noticed developers and content authors manually compiling a list of item paths to include in release notes.

I've come across a scenario where over 50 items must be transferred from one environment to another. This can be a time-consuming process with potential for mistakes. To tackle this, we are considering automating the task using Sitecore's SPE module and improving it with custom scripts or alternative methods.

Enhance script from SPE module

Custom non SPE way

Quick start

SPE module available and instance in AS environments.

Need custom .net code and configuration project in back-end

Reading item paths list from CSV

Reading data in SPE

reading data in back-end .net code

Support and Maintenance

Code change & deployment is not required, manageable from CMS

requires code change/deployment.

Source control

Yes, TDS project

Yes, .net Project

Sitecore PowerShell Extensions (SPE) is a powerful module that allows developers and administrators to automate many tasks in Sitecore. It provides a scripting language that can be used to interact with the Sitecore API and perform various operations on items, templates, layouts, and other Sitecore components.

The process can be made more effective and less error-prone by utilizing the SPE module to create scripts that can package multiple items at once. Furthermore, we can add special logic or functionality to these scripts to improve them even further and speed up the procedure.

One method for automating the creation of Sitecore packages is to use the Sitecore PowerShell module to read item paths from a CSV file with a true or false column to include child items.

Using the Sitecore PowerShell module, one can automate Sitecore package creation by reading item paths from a CSV file with a true/false column to indicate whether or not child items should be included.

  1. Install Sitecore PowerShell Extensions: Firstly, install the Sitecore PowerShell Extensions (SPE) module from the Sitecore Marketplace. This module provides a set of PowerShell cmdlets that allow us to manage Sitecore items, create packages, and more.

  2. Prepare the CSV file: Prepare the CSV file with a true or false column for including child items, and save it in a location that is accessible by the Sitecore server. The CSV file should have the following columns: Item Path, Include Child Items (true/false).

  3. Create a PowerShell script: Create a PowerShell script that reads the CSV file and creates Sitecore packages based on the item paths and child item inclusion specified in the file. Here's an example script that you can modify to fit your specific requirements:

Import-Module -Name Sitecore PowerShell Extensions

$csvFilePath = "C:\csv\items.csv"
$packageName = "MyPackage"
$packagePath = "/sitecore/system/Packages/MyPackage"

$items = Import-Csv $csvFilePath

$package = New-Item -ItemType "Package" -Name $packageName -Path $packagePath

foreach ($item in $items) {
    $itemPath = $item."Item Path"
    $includeChildItems = [bool]($item."Include Child Items")
    
    $item = Get-Item -Path $itemPath
    
    if ($item -ne $null) {
        $package.AddItem($item)
        
        if ($includeChildItems) {
            $childItems = Get-ChildItem -Path $itemPath -Recurse
            
            foreach ($childItem in $childItems) {
                $package.AddItem($childItem)
            }
        }
    }
}

$package.Close()

This script reads the CSV file located at $csvFilePath, creates a new Sitecore package with the name $packageName and saves it to the path $packagePath. It then iterates through each item in the CSV file, adds it to the package, and adds its child items if specified.

Appendices:

https://github.com/hishaamn/SitecorePackageAutoGenerator

https://www.flux-digital.com/blog/scripting-sitecore-packages-spe/

https://sitecorediaries.org/2020/07/03/creating-package-using-sitecore-powershell-extensions-spe-for-multiple-items/

https://doc.sitecorepowershell.com/appendix/packaging/new-package

Comments