Skip to content

Instantly share code, notes, and snippets.

@ezlateva
Last active August 16, 2016 21:47
Show Gist options
  • Select an option

  • Save ezlateva/90a3e05702389b9805db27503bd3d3fc to your computer and use it in GitHub Desktop.

Select an option

Save ezlateva/90a3e05702389b9805db27503bd3d3fc to your computer and use it in GitHub Desktop.
A TDS Post Deploy Step for triggering a site publish
using HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor.Contracts;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using Sitecore;
using Sitecore.Collections;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Managers;
using Sitecore.Globalization;
using Sitecore.Publishing;
namespace Hedgehog.TDS.Extensions
{
[Serializable]
[Description("Triggers a full site publish after a deployment.\nYou can specify a 'language', 'target', and 'mode' parameters.\nMode can be either 'full', 'smart', or 'incremental' (defaults to 'smart' if left out).\nIf no parameters are provided, a smart publish will be triggered in all languages for all publishing targets.")]
public class PublishSite : IPostDeployAction
{
public void RunPostDeployAction(XDocument deployedItems, IPostDeployActionHost host, string parameter)
{
NameValueCollection parameters = HttpUtility.ParseQueryString(parameter ?? string.Empty);
Database sourceDatabase = Database.GetDatabase("master");
if (sourceDatabase == null)
{
throw new InvalidOperationException("Could not locate publishing source (master) database.");
}
string mode = parameters["mode"] ?? "smart";
// languages
string[] languageParameters = parameters["language"] != null ? parameters["language"].Split(',') : new string[] { };
Language[] languages = languageParameters.Select(LanguageManager.GetLanguage).ToArray();
if (languages.Length == 0)
{
// default to all languages
languages = LanguageManager.GetLanguages(sourceDatabase).ToArray();
}
// target databases
string[] targetParameters = parameters["target"] != null ? parameters["target"].Split(',') : new string[] { };
Database[] targetDatabases;
if (targetParameters.Length > 0)
{
targetDatabases = targetParameters.Select(Factory.GetDatabase).ToArray();
}
else
{
// default to all targets
targetDatabases = GetPublishingTargets(sourceDatabase).ToArray();
}
try
{
switch (mode)
{
case "full":
PublishManager.Republish(sourceDatabase, targetDatabases, languages, Context.Language);
break;
case "incremental":
PublishManager.PublishIncremental(sourceDatabase, targetDatabases, languages,
Context.Language);
break;
case "smart":
default:
PublishManager.PublishSmart(sourceDatabase, targetDatabases, languages, Context.Language);
break;
}
host.LogMessage("[PublishSitePostDeployAction] executing {0} publish from source: {1}, in languages: {2}, on targets: {3}",
mode, sourceDatabase, string.Join(",", languages.ToList()), string.Join(",", targetDatabases.ToList()));
}
catch (Exception ex)
{
host.LogMessage("[PublishSitePostDeployAction] Error publishing site: {0}", ex);
}
}
private IEnumerable<Database> GetPublishingTargets(Database sourceDatabase)
{
ItemList targets = PublishManager.GetPublishingTargets(sourceDatabase);
foreach (Item targetItem in targets)
{
Database database = Factory.GetDatabase(targetItem[FieldIDs.PublishingTargetDatabase]);
if (database != null)
{
yield return database;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment