Packaging
Ever wanted to package up items and files without opening the Sitecore Package Designer each time? There are a number of commands available for generating packages.

Package Creation

Example: The following example demonstrates how to generate a package.
1
$package = New-Package "Package-of-Stuff"
2
$package.Sources.Clear()
3
4
$package.Metadata.Author = "Michael West"
5
$package.Metadata.Publisher = "Team Awesome"
6
$package.Metadata.Version = "1.0"
7
$package.Metadata.Readme = @"
8
Set of instructions for the user.
9
"@
10
11
# Items using New-ItemSource and New-ExplicitItemSource
12
$source = Get-Item -Path "master:\templates\Feature\Forms" |
13
New-ItemSource -Name 'Feature Forms Items' -InstallMode Overwrite
14
$package.Sources.Add($source)
15
16
# Files using New-FileSource and New-ExplicitFileSource
17
$source = Get-Item -Path "$AppPath\App_Config\Include\Feature\Forms\Company.Feature.Forms.config" |
18
New-ExplicitFileSource -Name "Feature Forms Files"
19
$package.Sources.Add($source)
20
21
Export-Package -Project $package -Path "$($package.Name)-$($package.Metadata.Version).xml"
22
Export-Package -Project $package -Path "$($package.Name)-$($package.Metadata.Version).zip" -Zip
23
Download-File "$SitecorePackageFolder\$($package.Name)-$($package.Metadata.Version).zip"
Copied!

Post Step

Example: The following adds a Post Step and custom attributes.
1
$package = New-Package "Package-of-Stuff"
2
$package.Sources.Clear()
3
4
$package.Metadata.Author = "Michael West"
5
$package.Metadata.Publisher = "Team Awesome"
6
$package.Metadata.Version = "1.0"
7
$package.Metadata.Readme = @"
8
Set of instructions for the user.
9
"@
10
$package.Metadata.PostStep = "Some.Library.Class,Some.Library"
11
$package.Metadata.Attributes = "scriptId={9b9a3906-1979-11e7-8c9d-177c30471cec}|width=50|height=200"
12
13
Export-Package -Project $package -Path "$($package.Name)-$($package.Metadata.Version).xml"
Copied!
Example: The following adds a Post Step included with SPE to delete a file.
1
$package = New-Package "Package-of-Stuff"
2
$package.Sources.Clear()
3
4
$package.Metadata.Author = "Michael West"
5
$package.Metadata.Publisher = "Team Awesome"
6
$package.Metadata.Version = "1.0"
7
$package.Metadata.Readme = @"
8
Set of instructions for the user.
9
"@
10
$newPackageFiles = @([PSCustomObject]@{"FileName"="/bin/Company.Feature.Unused.dll"})
11
$package.Metadata.PostStep = "Spe.Package.Install.PackagePostStep, Spe.Package"
12
$package.Metadata.Comment = New-PackagePostStep -PackageFiles $newPackageFiles
Copied!
The function New-PackagePostStep used by the example above can be included in the same script.
1
function New-PackagePostStep {
2
param(
3
$PackageItems,
4
$PackageFiles
5
)
6
7
$writer = New-Object System.IO.StringWriter
8
$output = New-Object System.Xml.XmlTextWriter([System.IO.TextWriter]$writer)
9
$output.Formatting = [System.Xml.Formatting]::Indented
10
$output.WriteStartElement("uninstall")
11
12
if($PackageItems) {
13
$output.WriteStartElement("items")
14
foreach($packageItem in $PackageItems) {
15
$output.WriteStartElement("item")
16
$output.WriteAttributeString("database", $packageItem.Database)
17
$output.WriteAttributeString("id", $packageItem.ID.ToString())
18
$output.WriteEndElement()
19
}
20
$output.WriteEndElement()
21
}
22
if($PackageFiles) {
23
$output.WriteStartElement("files")
24
foreach($packageFile in $PackageFiles) {
25
$output.WriteStartElement("file")
26
$output.WriteAttributeString("filename", $packageFile.FileName)
27
$output.WriteEndElement()
28
}
29
$output.WriteEndElement()
30
}
31
32
$output.WriteEndElement()
33
$writer.ToString()
34
}
Copied!
Last modified 2yr ago
Export as PDF
Copy link