Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Create Package from Report CSV file
# Written by Rodrigo Peplau
# Use this SPE Script to convert a CSV file generated by SPE Reports into a Sitecore Package
# Create this script under the following path and it will become part of your Powershell Toolbox
# /sitecore/system/Modules/PowerShell/Script Library/Package Generator/Toolbox/Create Package from Report CSV file
# See details on
# Upload CSV file
[Sitecore.Data.Items.MediaItem] $uploadedItem = Receive-File -ParentItem (get-item "master:\media library\Files") -Overwrite -Title "Please upload your CSV file" -Description " "
# Get CSV content from it
$fileType = $uploadedItem.Extension
$fileName = $uploadedItem.DisplayName
if ($fileType.ToLower() -eq "csv"){
[Sitecore.Resources.Media.Media] $media = [Sitecore.Resources.Media.MediaManager]::GetMedia($uploadedItem);
$stream = $media.GetStream().Stream
$streamReader = New-Object System.IO.StreamReader($stream)
$content = $streamReader.ReadToEnd()
$csvArray = ConvertFrom-Csv -InputObject $content
else {
Show-Alert "Please upload a valid CSV file!"
Remove-Item -Path $uploadedItem.InnerItem.Paths.Path
return $null
# Delete CSV file
Remove-Item -Path $uploadedItem.InnerItem.Paths.Path
# Skip if content was not loaded
if ($csvArray -eq $null){
Show-Alert "The uploaded CSV file is invalid!"
return $null
# Create package
$packageName = $fileName
$package = New-Package -Name $packageName
# Set package metadata
$package.Name = $packageName
$package.Metadata.Author = "Sitecore Powershell Extensions";
$package.Metadata.Publisher = "Sitecore Powershell Extensions";
$package.Metadata.Version = "1.0";
$package.Metadata.Readme = "Content exported from file $($packageName).csv"
# Add content to the package
foreach ($csvLine in $csvArray)
$itemPath = $csvLine.Path
$item = Get-Item "master:$itemPath" -erroraction stop
Write-Log "Item not found: $itemPath, message: $_"
Write-Host "Item not found: $itemPath, message: $_"
if ($item -ne $null)
$installMode = $csvLine.InstallMode
$mergeMode = $csvLine.MergeMode
if ($installMode -eq $null -or $installMode -eq ""){
$installMode = "Merge"
if ($mergeMode -eq $null -or $mergeMode -eq ""){
$mergeMode = "Merge"
$installMode = [Sitecore.Install.Utils.InstallMode]$installMode
$mergeMode = [Sitecore.Install.Utils.MergeMode]$mergeMode
$sourceName = $csvLine.Source
if ([System.String]::IsNullOrEmpty($sourceName)){
$sourceName = $item.Paths.Path
$isNewSource = $true
# Try to get from existing source
$source = $package.Sources | Where-Object {$_.Name -eq $sourceName}
if ($source -ne $null){
$isNewSource = $false
# Create new source
if ($isNewSource -eq $true){
if ($installMode -eq [Sitecore.Install.Utils.InstallMode]::Merge){
$source = New-ExplicitItemSource -Item $item -Name $sourceName -InstallMode $installMode -MergeMode $mergeMode
else {
$source = New-ExplicitItemSource -Item $item -Name $sourceName -InstallMode $installMode
Write-Host "Added to package: $($item.Paths.FullPath)"
Write-Log "Added to package: $($item.Paths.FullPath)"
else {
Show-Alert "Item not found: $($itemPath)"
# Save package
$allSources = $package.Sources
$packagePath = "$($package.Name)-$($package.Metadata.Version)"
$packagePathZip = "$"
$locationPackage = "$SitecorePackageFolder\$packagePathZip"
$package | Export-Package -Path "$packagePath.xml"
Export-Package -Project $package -Path $packagePathZip -Zip:$true -IncludeProject:$true
# Offer the user to download the package
Download-File "$SitecorePackageFolder\$"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment