Post Install Publisher

One thing that frustrates me when dealing with large Sitecore packages is not knowing where items have been installed and not wanting to do a full site publish on a live site with over 30,000 items, and possible draft content.

So i have put together a simple event handler to publish all the items in the package, to all targets in all languages once the package has finished installing.

nothing to complicated, code and configuration below, or for simplicity you can download the sitecore package here or from the Sitecore Marketplace, tested against Sitecore v7/v8

code:

using Sitecore.Data;
using Sitecore.Data.Events;
using Sitecore.Data.Items;
using Sitecore.Data.Managers;
using Sitecore.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Arktouros_PostInstallPublish
{
 public class EventHandler
 {
 public virtual void PublishItems(object sender, System.EventArgs args)
 {
 try
 {
 SitecoreEventArgs evntArgs = (SitecoreEventArgs)args;
 Sitecore.Install.Events.InstallationEventArgs installEventArgs = (Sitecore.Install.Events.InstallationEventArgs)evntArgs.Parameters[0];

 Database master = Sitecore.Configuration.Factory.GetDatabase("master");
 //get publishing targets
 List targetDbs = new List();
 foreach (var trgt in Sitecore.Publishing.PublishManager.GetPublishingTargets(master))
 {
 if (!string.IsNullOrEmpty(trgt["Target Database"]))
 {
 Database target = Sitecore.Configuration.Factory.GetDatabase(trgt["Target Database"]);
 if (target != null)
 {
 targetDbs.Add(target);
 }
 }
 }
 //get languages
 Sitecore.Collections.LanguageCollection langs = LanguageManager.GetLanguages(master);
 List lstToPublish = new List();
 foreach (var i in installEventArgs.ItemsToInstall)
 {
 Item toPublish = master.GetItem(i.ItemID);
 if (toPublish != null)
 {
 lstToPublish.Add(toPublish);
 }
 }
 //order by axes so we make sure we publish any parent nodes first
 lstToPublish = lstToPublish.OrderBy(x => x.Axes.Level).ToList();
 //start publish job for each item, not the most efficient, but only way
 foreach (Item i in lstToPublish)
 {
 Sitecore.Publishing.PublishManager.PublishItem(i, targetDbs.ToArray(), langs.ToArray(), false, false, false);
 }
 }
 catch (Exception ex)
 {
 Sitecore.Diagnostics.Log.Error("Arktouros - PublishAfterInstall Failed: " + ex.Message, ex);
 }
 }
 }
}

configuration:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:x="http://www.sitecore.net/xmlconfig/">
 <sitecore database="SqlServer">
 <events timingLevel="custom">
 <event name="packageinstall:items:ended">
 <handler type="Arktouros_PostInstallPublish.EventHandler, Arktouros_PostInstallPublish" method="PublishItems" />
 </event>
 </events>
 </sitecore>
</configuration>

All done, enjoy

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 )

Google+ photo

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

Connecting to %s