Publish Bulk Items in Sitecore using Sitecore Powershell Extensions

Every Sitecore site will have a different set of requirement and one of the requirement could be Publish Bulk Items in Sitecore. If you have the same requirement then you are at right place. ๐Ÿ™‚

Publish Bulk Items In Sitecore

 

This post is in accordance with my previous two posts:

  1. Export Sitecore Items
  2. Update Sitecore Items

This might be the case for your requirement, first Export Sitecore Items, then it could be Update Sitecore Items in bulk and then I’m sure there would be the third one to Publish Sitecore Items in Bulk.

I’ve written Sitecore Powershell script which would do the job easier. In Update Sitecore Itemsย — I provided the sample file, the same file you can use for Bulk Publish.

Sample CSV File: pt_Publish_Sitecore_Items (I generally prefix the file with language so that we know which language version items are getting published)

Below script will first ask the user for the CSV File. Then it will ask for Target Database to publish Sitecore Items — The reason for providing this option is because every Sitecore site have the staging environment and it’s always better to verify the changes. So you can accordingly select the target database and after verifying you can push it Live. Once processing is done file will be deleted. Publish Mode is set to Smart.

Note, Sample CSV File has two important columns which are required by this script, ItemPath and Language — Items will be published in the provided language.

Execute the Script in Sitecore PowerShell Console or PowerShell ISE

#Publish Bulk Items In Sitecore Using Sitecore PowerShell Extensions

#Upload the file on the Server in temporary folder
#It will create the folder if it is not found
$dataFolder = [Sitecore.Configuration.Settings]::DataFolder
$tempFolder = $dataFolder + "\temp\upload"
$filePath = Receive-File -Path $tempFolder -overwrite

if($filePath -eq "cancel"){
    exit
}

$resultSet =  Import-Csv $filePath

$rowsCount = ( $resultSet | Measure-Object ).Count;

    if($rowsCount -le 0){
        #If there is no record in csv file then remove the file and exit
        Remove-Item $filePath
        exit
    }

$publishingTargetsFolderId = New-Object Sitecore.Data.ID "{D9E44555-02A6-407A-B4FC-96B9026CAADD}"
$targetDatabaseFieldId = New-Object Sitecore.Data.ID "{39ECFD90-55D2-49D8-B513-99D15573DE41}"

# Find the publishing targets item folder
$publishingTargetsFolder = [Sitecore.Context]::ContentDatabase.GetItem($publishingTargetsFolderId)

if ($publishingTargetsFolder -eq $null -Or !$publishingTargetsFolder.HasChildren) {
    $logInfo = "Publish Bulk Items - No Target Database Found."
    $logInfo
    Write-Log $logInfo 
    exit
}

#Hashtable
$targetDatabases = @{}

foreach($publishingTargetDatabase in $publishingTargetsFolder.GetChildren()) {
    $targetDatabases[$publishingTargetDatabase[$targetDatabaseFieldId]] = $publishingTargetDatabase[$targetDatabaseFieldId] 
    $publishTarget = $publishingTargetDatabase[$targetDatabaseFieldId]
}

$props = @{
    Parameters = @(
        @{ Name = "publishSubitems"; Value=$false; Title="Publish subitems"; Tooltip="Publish subitems";}, 
        @{ Name="publishTarget"; Title="Target Database"; Options=$targetDatabases; Tooltip="Target Database for Publishing Items"; Editor="combobox"}
    )
    Title = "Publish Bulk Items"
    Description = "Publish Bulk Items"
    ShowHints = $true
}

$result = Read-Variable @props

if($result -eq "cancel"){
    exit
}

if( $BulkPublishItems -eq ""){
    $logInfo = "No items published. Please try again and provide items Path for publishing!";
    $logInfo
    Write-Log $logInfo 
    exit
}

$logInfo = "Publish Bulk Items Started!";
$logInfo
Write-Log $logInfo

foreach ( $row in $resultSet ) {
        $currentItem = Get-Item -Path $row.ItemPath -Language $row.Language -ErrorAction SilentlyContinue
        if ($currentItem){
            if($publishSubItems){
                $currentItem | Publish-Item -Recurse -Language $row.Language -PublishMode Smart -Target $publishTarget
            }
            else{
                $currentItem | Publish-Item -PublishMode Smart -Target $publishTarget
            }
            $logInfo = "Item Published: " + $currentItem.Paths.FullPath;
            $logInfo
            Write-Log $logInfo
        }
        else {
            $logThis =  "Couldn't find: " + $row.ItemPath + " with language version: " + $row.Language 
            $logThis
            Write-Log $logThis
        }
    }
    
    $logInfo = "Publish Bulk Items Completed!";
    $logInfo
    Write-Log $logInfo

Remove-Item $filePath

You can modify the script as per your need! — If you want myย help you can anytime shout me loud by commenting below! ๐Ÿ˜‰

Screenshots:

Upload-CSV-File

Select-Target-Database

Output

Bulk Publishing

Reference:

http://www.sitecorenutsbolts.net/2015/12/14/Multi-Item-Publish-with-Sitecore-Powershell-Extensions/

If you don’t have Sitecore Powershell and still looking for some solution, you can read the post by Nilesh Thakkar:

http://sitecorejourney.nileshthakkar.in/2016/12/publish-bulk-items-in-sitecore-using.html

Happy Sitecoring! ๐Ÿ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.