Programmatically skip publishing of item(s) in Sitecore

by aboo bolaky 23. July 2009 05:43

Scenario

Assume that you have a set of items (say..Product items) (sitting anywhere within the /sitecore/content..) based on a specific template. The requirement here is that

"A Product cannot be published if one of its fields (ProductID) isn't populated."

Background

To achieve this, we need to hook into the publish:itemProcessing event in the web.config. This event gets triggered every time an item is published. The general steps involved in this situation are:

  • Create a class with a method that adheres to the EventHandler delegate signature. Whenever you initiate a publish operation in sitecore (be it smart publish, incremental or full publish), the method will be called (depending on how many items that need to be published)
  • Modify the web.config to subscribe to ItemPublishing event

ItemPublisher Class

namespace Test.Events 
{
	public class ItemPublisher
	{
	   public void CheckProcessing(object sender, EventArgs args)
		{
		  ItemProcessingEventArgs theArgs = args as ItemProcessingEventArgs;
						
		  Item currentItem = theArgs.Context.PublishHelper.GetSourceItem(theArgs.Context.ItemId);

		  if ((currentItem != null) && (currentItem.Paths.IsContentItem))
		  {
			  //Template ID of item on which selective publishing is to be applied
			 if (currentItem.TemplateID == new ID("{9C9A2F3D-652A-4490-AB57-E9F1B4D5BF05}"))
			  {
				 Job currentJob = theArgs.Context.Job;
				 JobStatus currentJobStatus = currentJob.Status;

				 if (String.IsNullOrEmpty((currentItem.Fields["Product ID"].Value)))
				  {
					currentJobStatus.Messages.Add(String.Format("Item :{0} has not been published since it has no Product ID", currentItem.Name));
					theArgs.Cancel = true;
					return;
				   }
			  }
		   }
		}
	}
}


Line 7:
Cast the standard EventArgs class to ItemProcessingEvent. This is important since it gives you the possibility of retrieving details of the items being published.

Line 9: Retrieve the item being published.

Line 11: The check for "currentItem.Paths.IsContentItem" is important since we only want to check for content items. Since publishable items sitecore vary from templates,standard values, renderings... we do not want to check for the condition in ALL items.

Line 14: If the template of the current item matches the id of the Product template, then we're back in business.

Line 16 - 21: Find the reference to the current Job (and JobStatus)  being executed in the publish pipeline. If the item's field is empty, add a message to the JobStatus.

Line 22: Abort the publish operation of the current item. I don't think we require the return statement after that. Publishing will then resume for the next item in the publishing queue.

Web.Config Change

Locate the publish:itemProcessing event in the web.config. Hook up the new handler to the event.

<event name="publish:itemProcessing" 
help="Receives an argument of type ItemProcessingEventArgs (namespace: Sitecore.Publishing.Pipelines.PublishItem)" >
   <handler type="Test.Events.ItemPublisher,TestApplication" method="CheckProcessing" />
</event>

Let's put it to the test

That's all to it really. If you now  initiate a publish operation and one of the Products has an emtpy Product ID, you will end up with this (if you click on "Click here to see additional information" on the last screen of the publish wizard.)

Items Skipped = 1 (.i.e Camera item has been skipped during the publish process since it has no ProductID). If you switch to the web database, there will not be any "Camera" item. 

Result... :)

Tags: ,

.Net | Applications | Sitecore

Comments

5/19/2010 12:19:45 PM #

latin shoes

thanks for your article

latin shoes United States |

5/19/2010 12:32:06 PM #

latin shoes

thanks for your article

latin shoes United States |

5/20/2010 7:11:18 PM #

MBT SHOES

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.

MBT SHOES United States |

11/5/2011 6:15:50 AM #

louer riad marrakech

Enjoyed lesson done this, much estimable material, blesss .

louer riad marrakech France |

11/7/2011 10:44:55 AM #

Marrakech

As I website owner I comprehend the satisfy here is palpable captivating , esteems for your attempts.

Marrakech France |

11/7/2011 12:35:42 PM #

Voyage Marrakech

Thanks for the auspicious writeup. It really was previously a satisfaction tale it. Skip sophisticated to widely delivered appealing from you! Yet, how can we restrain up a agreement?

Voyage Marrakech France |

11/13/2011 4:40:50 AM #

Marrakech

I loved as many as you testament derive carried away exactly here. The blueprint is savory, your authored matter dapper. nevertheless, you commission secure got an edginess across that you covet be turning in the consequent. unpropitious indubitably happen further in the yesteryear quondam further as right the tantamount almost precise continuously internal case you vindicate this increase.

Marrakech France |

11/13/2011 9:44:04 AM #

Tout Marrakech

Enjoyed reading through this, very good stuff, thanks .

Tout Marrakech France |

11/13/2011 11:39:53 AM #

Marrakech

Precise stimulating junctures you possess renowned , admire it for posting . “Success is a migration, hardly a aim. The doing is frequent further noteworthy than the consequence.” by Arthur Ashe.

Marrakech France |

11/15/2011 1:06:08 PM #

promoteur immobilier maroc

I gotta favorite this spot it seems exceedingly useful exceedingly useful

promoteur immobilier maroc France |

11/15/2011 10:12:15 PM #

annonces immobilieres marocaines

I like what you guys are up plus. Such artful process besides reporting! Guard up the famous processs mans I’ve incorporated you mans to my blogroll. I expect it bequeath benefit the estimate of my location Sourire . “A enclosure is a install where what was foggy previous betides further undecided than always.” by Henry Waldorf Francis.

annonces immobilieres marocaines France |

11/16/2011 1:59:52 AM #

doha immobilier maroc

Good blog! I indeed tenderness how it is compliant on my orbs besides the information are well written. I’m wondering how I power be notified whenever a pristine dispatch has been made. I possess subscribed to your RSS which essential do the dupe! Possess a huge age! “Reputation is what further folk understand about you. Hail is what you understand about yourself.” by Lois McMaster Bujold.

doha immobilier maroc France |

Tag cloud

Flash Player 9 required.

About Me

I wish I could write something here..
//TODO: ElaborateMe