Sitecore – Unpublish archived items programmatically

When you archive an item in your master database, Sitecore doesn’t remove it from the web database automatically. You need to perform a publish in order to “update” your web instance. That’s why this post will show you how to unpublish items immediately when an item is archived.

If you archive an item, you will notice that your item will be “moved” from your tree node to the archived items box. (Start -> All Applications -> Archive)

ezgif.com-resize

…but if you select the web database, you will verify that your archived item is still there which means it currently lives on your website.

web

Sometimes this behavior is not desired and maybe we want to unpublish the item automatically at the same time we archived it. This can be done easily with a custom pipeline.

namespace Website.Pipelines
{
	public class UnpublishArchivedItem : DeleteItems
    {
		public void Process(ClientPipelineArgs args)
		{
		    Assert.ArgumentNotNull(args, "args");
		    Database database = Factory.GetDatabase(args.Parameters["database"]);
		    Assert.IsNotNull(database, typeof(Database), "Name: {0}", args.Parameters["database"]);
            ListString listStrings = new ListString(args.Parameters["items"], '|');
            
            Database target = Factory.GetDatabase("web"); 

            foreach (string listString in listStrings)
		    {
		        Item item = database.GetItem(listString, Language.Parse(args.Parameters["language"]));
		        if (item == null)
		        {
		            continue;
		        }
                Log.Audit(this, "Unpublish item: {0}", new string[] { AuditFormatter.FormatItem(item) });

                item.Editing.BeginEdit();
		        item.Publishing.NeverPublish = true;
		        item.Editing.EndEdit();

		        PublishManager.PublishItem(item.Parent, new []{ target }, item.Languages, true, false);
		    }
        }
	}
}

The following code will process all the selected items to be archived and will unpublish them from the web database. It will take the item, will set as unpublishable and finally publish its parent.

Then, we need to hook this pipeline into the archive items flow, which means we need to create a patch file to add this custom pipeline and place it into the correct processor.



  
    
      
        
      
   
  

This configuration will add the pipeline just before the item is archived. We unpublish the item before the item is archived since after that we can no longer manage the context item in the master database.

ShowConfig.aspx

Now, if you archive an item you will see the item is removed from the web database too and it’s no longer visible on the website anymore.

NOTE: Alternatively, we could create a custom pipeline to set the ‘Never Publish’ to false when the item is restored.

Hope this little article helps you somehow.

Happy Sitecoreing 😉

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 )

Facebook photo

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

Connecting to %s