UnPublish Sitecore items in a click !

No comments

I have seen a lot of content authors making a mistake of deleting an item in CMS before un-publishing it. This makes the item to persist in Target Database (usually the Web Database) as well as in search indexes. 

To prevent this common mistake and to make the un-publishing easier, I created a Sitecore Powershell Module with confirmation. 

Thanks to Sitecore Powershell Extensions, it took just couple of hours to implement this. Thanks to Sitecore Powershell Extensions

You can download Sitecore package to quickly install it in your Sitecore instance.

Let’s see in detail how it is created:

  • Go to System/Modules/Powershell/Script Library and right click it.
    Under insert options, click on Module Wizard.
Image 1: Creating Powershell module using SPE
  • It opens SPE’s Module Wizard, provide a name for the module and choose the Integration points to create. As I wanted it to appear only in Content Editor Ribbon, I chose only this option. 
Image 2: Select Integration Points to Create
  • Once the module finishes running , it creates a complete list of Menus that are available in Core DB.  Add Powershell script through insert options under the desired menu section or chunk and delete the others chunks which you do not require.

Image 3: Insert PS script for each button
  • After deleting unused chunks, configure icons for each buttons to make it user friendly.
Image 4: Configure Icons
  • Next step is to update the PS Script for each button. To do this, open Powershell ISE and select button script .
Image 5: Open Scripts in Powershell ISE
  • Update below scripts for respective buttons. The initial script of un-publishing was taken from here and improved to have SPE’s Interactive dialogs like Show-Confirm , which has Ok and Cancel button and Show-Alert , which displays alert message.
#UNPUBLISH AND DELETE BUTTON

#Getting selected Item
$sourceItem = Get-Item .

$displayMsg = "Are You Sure you want to Un-Publish and Delete  '" + $sourceItem.Name + "' Item ?"

#Confirmation Dialog with Ok / Cancel Button
$ReturnValue = Show-Confirm -Title $displayMsg

function RunScript
{
    #Making item unpublishable 
    $sourceItem.Editing.BeginEdit();
    $sourceItem.Fields["__Never publish"].Value = "1";
    $sourceItem.Editing.EndEdit();
    
    #publishing item to web db in all languages - You can update desired target, language and publishing type below
    Publish-Item $sourceItem -Target "web" -Language * -PublishMode Smart 
    
    #Removing the Item
    $sourceItem | Remove-Item
}


#If Ok button is chosen it returns 'yes' which in turn runs the below script
if($ReturnValue -eq 'yes'){
    $items = RunScript
    $displayMsg =  $sourceItem.Name + " Item has been Unpublished and Deleted !"
    Show-Alert -Title $displayMsg
}


#UN-PUBLISH Child Items

#Getting selected Item
$sourceItem = Get-Item .

$displayMsg = "Are You Sure you want to Un-Publish Child Items of  '" + $sourceItem.Name + "' Item ?"

#Confirmation Dialog with Ok / Cancel Button
$ReturnValue = Show-Confirm -Title $displayMsg

function RunScript
{
    #Unpublishing and Removing Child Items
    $items = Get-ChildItem -Path $sourceItem.ItemPath -Recurse
    foreach ($item in $items)
    {
            $item.Editing.BeginEdit();
            $item.Fields["__Never publish"].Value = "1";
            $item.Editing.EndEdit();

            Publish-Item $item -Target "web" -Language * -PublishMode SingleItem 
    }
}
#If Ok button is chosen it returns 'yes' which in turn runs the below script
if($ReturnValue -eq 'yes'){
    $items = RunScript
    $displayMsg =  $sourceItem.Name + " Item's children have been unpublished successfully!!"
    Show-Alert -Title $displayMsg
}

#UN-PUBLISH Selected Item

#Getting Selected Item
$sourceItem = Get-Item . -Language *
#Confirmation Dialog with Ok / Cancel Button
$displayMsg = "Are You Sure you want to Un-Publish " + $sourceItem[0].Name + " Item ?"
$ReturnValue = Show-Confirm -Title $displayMsg
function RunScript
{
    foreach ($item in $sourceItem)
    {
        $item.Editing.BeginEdit();
        $item.Fields["__Never publish"].Value = "1";
        $item.Editing.EndEdit();
    
        Publish-Item $item -Target "web" -PublishMode Smart 
    }
}
#If Ok button is chosen it returns 'yes' which in turn runs the below script
if($ReturnValue -eq 'yes'){ 
    $items = RunScript
    $displayMsg =  $sourceItem[0].Name + " Item has been unpublished successfully!!"
    Show-Alert -Title $displayMsg
}



  • Click on Settings , select drop down next Rebuild All Integration Points and click on “Sync Library with Content Editor Ribbon“. This will sync the changes with Core DB.
Image 6: Sync Library
  • After Sync, goto Core DB and update Tool Tip as it cannot be done using Sync Library option.
Image 7: Update Tool Tip
  • As a final step, assign security rights for each buttons. In my case, I wanted to provide “Unpublish Item and Delete” feature only for admin users whereas provide Unpublish for all content authors.

Image 8: Unpublish Delete Button Enabled for Admins
Image 9: Unpublish Delete Button DISABLED for non-admins

I hope it would make your un-publishing easier !! You can also download the package here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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