Skip to content

Instantly share code, notes, and snippets.

@sohelsd
Created July 27, 2015 19:22
Show Gist options
  • Save sohelsd/3f5f132c87a73cd2a953 to your computer and use it in GitHub Desktop.
Save sohelsd/3f5f132c87a73cd2a953 to your computer and use it in GitHub Desktop.
Sitecore Package Installer through code. You store packages under /Packages folder. Call the controller through /utility/installpackage.
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Site.Web
{
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("InstallPack","utility/installpackage", new {controller = "Utility", action = "InstallPackage"});
}
}
}
using System;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using Sitecore.Data.Engines;
using Sitecore.Data.Proxies;
using Sitecore.Exceptions;
using Sitecore.Install.Files;
using Sitecore.Install.Framework;
using Sitecore.Install.Items;
using Sitecore.Install.Utils;
using Sitecore.Publishing;
using Sitecore.SecurityModel;
namespace Site.Web.Controllers.Shared
{
public class UtilityController: Controller
{
public JsonResult InstallPackage()
{
var installer = new PackageInstaller();
var hasChanges = false;
var logFile = Sitecore.MainUtil.MapPath("/Packages/PackageInstallLog.txt");
var status = string.Empty;
if (!System.IO.File.Exists(logFile))
{
try
{
System.IO.File.Create(logFile).Close();
}
catch (Exception ex)
{
status = "Installation failed. " + ex;
}
}
//Get Files
var fileEntries = Directory.GetFiles(Sitecore.MainUtil.MapPath("/Packages"));
var logEntries = System.IO.File.ReadAllLines(logFile);
//Install Plugins
foreach (var fileName in fileEntries)
{
if (fileName == logFile)
continue;
var isInstalled = false;
foreach (var installedPackage in logEntries)
{
if (fileName.Equals(installedPackage))
isInstalled = true;
}
if (!isInstalled)
{
try
{
installer.InstallPackage("/Packages/" + fileName.Split('\\').Last());
System.IO.File.AppendAllLines(logFile, new[] { fileName });
status += "Installed: " + fileName + "<br/>";
hasChanges = true;
}
catch (Exception ex)
{
status = "Installation failed" + ex;
}
}
}
status += "<br/>Installation succeeded";
if (!hasChanges)
status = "You are up-to-date!";
return new JsonResult
{
Data = status,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
}
class PackageInstaller
{
public void InstallPackage(string path)
{
Sitecore.Context.SetActiveSite("shell");
using (new SecurityDisabler())
{
using (new ProxyDisabler())
{
using (new SyncOperationContext())
{
IProcessingContext context = new SimpleProcessingContext();
IItemInstallerEvents events = new DefaultItemInstallerEvents(new BehaviourOptions(InstallMode.Overwrite, MergeMode.Undefined));
context.AddAspect(events);
IFileInstallerEvents events1 = new DefaultFileInstallerEvents(true);
context.AddAspect(events1);
var inst = new Sitecore.Install.Installer();
var filePath = Sitecore.MainUtil.MapPath(path);
var pkgFile = new FileInfo(filePath);
if (!pkgFile.Exists)
throw new ClientAlertException(string.Format("Cannot access path '{0}'. Please check path setting.", path));
inst.InstallPackage(filePath, context);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment