Make Sitecore stop publishing certain items

This is part 2 of adding a second master database. When having a second database with the purpose of holding millions of items and not needing publish since it will contain information from third party systems such as DAM or PIM we need to stop the publisher from publishing the information that is linked in from the second master database.

The procedure is fairly simple. First we need a handler that we can hook up in the web.config. So let’s create it.

public class ItemProcessing
  {

    public void CheckIfAsset(object sender, EventArgs args)
    {
      var theArgs = args as ItemProcessingEventArgs;
      var item = theArgs.Context.PublishHelper.GetSourceItem(theArgs.Context.ItemId);
      if (item.TemplateName == "MyAssetTemplate" || item.TemplateName == "MyAssetFolderTemplate")
      {
        theArgs.Cancel = true;
      }
    }

  }

What we are doing here is checking if the item that is about to be published is created from the template “MyAssetTemplate” or “MyAssetFolderTemplate”. If it is created from either of those we cancel the publishing of the item.

Now all that is left to do is to hook it up in an include file.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
      <event name="publish:itemProcessing" help="Receives an argument of type ItemProcessingEventArgs (namespace: Sitecore.Publishing.Pipelines.PublishItem)">
        <handler type="MyNamespace.Handlers.ItemProcessing, MyNamespace" method="CheckIfAsset" />
      </event>
    </events>
  </sitecore>
</configuration>

And that is it. Now items of those two template types should not be published. More to come in this series on hooking up a second master database with millions of items.

Leave a Reply

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