Skip to content

Instantly share code, notes, and snippets.

@peplau
Last active February 29, 2024 20:56
Show Gist options
  • Save peplau/46baed1ef28aae03f91b9ef07b7a168d to your computer and use it in GitHub Desktop.
Save peplau/46baed1ef28aae03f91b9ef07b7a168d to your computer and use it in GitHub Desktop.
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 https://www.nishtechinc.com/blog/2020/April/Advanced-content-packaging-with-Powershell
# 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()
$streamReader.Dispose()
$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.Sources.Clear();
$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)
{
try
{
$itemPath = $csvLine.Path
$item = Get-Item "master:$itemPath" -erroraction stop
}
catch
{
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
$source.Entries.Add([Sitecore.Install.Items.ItemReference]::New($item).ToString())
}
# 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
}
$package.Sources.Add($source);
}
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 = "$packagePath.zip"
$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\$packagePath.zip"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment