Skip to content

Instantly share code, notes, and snippets.

@mskutta
Last active March 21, 2019 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mskutta/c9fe547ac03895f3b087 to your computer and use it in GitHub Desktop.
Save mskutta/c9fe547ac03895f3b087 to your computer and use it in GitHub Desktop.
Sitecore Update and Zip Package Installer Web Service
<%@ WebService Language="C#" Class="PackageInstaller" %>
using System;
using System.Configuration;
using System.IO;
using System.Web.Services;
using System.Xml;
using Sitecore.Data.Proxies;
using Sitecore.Data.Engines;
using Sitecore.Install.Files;
using Sitecore.Install.Framework;
using Sitecore.Install.Items;
using Sitecore.SecurityModel;
using Sitecore.Update;
using Sitecore.Update.Installer;
using Sitecore.Update.Installer.Utils;
using Sitecore.Update.Utils;
using log4net;
using log4net.Config;
/// <summary>
/// Summary description for UpdatePackageInstaller
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class PackageInstaller : System.Web.Services.WebService
{
/// <summary>
/// Installs a Sitecore Update Package.
/// </summary>
/// <param name="path">A path to a package that is reachable by the web server</param>
[WebMethod(Description = "Installs a Sitecore Update Package.")]
public void InstallUpdatePackage(string path)
{
// Use default logger
var log = LogManager.GetLogger("root");
XmlConfigurator.Configure((XmlElement)ConfigurationManager.GetSection("log4net"));
var file = new FileInfo(path);
if (!file.Exists)
throw new ApplicationException(string.Format("Cannot access path '{0}'.", path));
using (new SecurityDisabler())
{
var installer = new DiffInstaller(UpgradeAction.Upgrade);
var view = UpdateHelper.LoadMetadata(path);
//Get the package entries
bool hasPostAction;
string historyPath;
var entries = installer.InstallPackage(path, InstallMode.Install, log, out hasPostAction, out historyPath);
installer.ExecutePostInstallationInstructions(path, historyPath, InstallMode.Install, view, log, ref entries);
UpdateHelper.SaveInstallationMessages(entries, historyPath);
}
}
/// <summary>
/// Installs a Sitecore Zip Package.
/// </summary>
/// <param name="path">A path to a package that is reachable by the web server</param>
[WebMethod(Description = "Installs a Sitecore Zip Package.")]
public void InstallZipPackage(string path)
{
// Use default logger
var log = LogManager.GetLogger("root");
XmlConfigurator.Configure((XmlElement)ConfigurationManager.GetSection("log4net"));
var file = new FileInfo(path);
if (!file.Exists)
throw new ApplicationException(string.Format("Cannot access path '{0}'.", path));
Sitecore.Context.SetActiveSite("shell");
using (new SecurityDisabler())
{
using (new ProxyDisabler())
{
using (new SyncOperationContext())
{
IProcessingContext context = new SimpleProcessingContext(); //
IItemInstallerEvents events =
new DefaultItemInstallerEvents(new Sitecore.Install.Utils.BehaviourOptions(Sitecore.Install.Utils.InstallMode.Overwrite, Sitecore.Install.Utils.MergeMode.Undefined));
context.AddAspect(events);
IFileInstallerEvents events1 = new DefaultFileInstallerEvents(true);
context.AddAspect(events1);
var installer = new Sitecore.Install.Installer();
installer.InstallPackage(Sitecore.MainUtil.MapPath(path), context);
}
}
}
}
}
@Saeedur
Copy link

Saeedur commented Apr 9, 2018

Will this code work with Sitecore 9.1? Thanks.

@rsenk
Copy link

rsenk commented May 15, 2018

For Sitecore 9.x please remove ProxyDisabler.

@Jonne
Copy link

Jonne commented Mar 21, 2019

InstallZipPackage does not import the user accounts, had to add the following code:

        IProcessingContext accountContext = new SimpleProcessingContext();
        IAccountInstallerEvents accountEvents = new DummyAccountInstallerEvents();
        accountContext.AddAspect<IAccountInstallerEvents>(accountEvents);
        
        new Installer().InstallSecurity(package, accountContext);
    public class DummyAccountInstallerEvents : IAccountInstallerEvents
    {
        public void ShowWarning(string message, string warningType)
        {
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment