How to upload Sitecore TDS package programmatically

We live in a world of automation. Everything we are doing manually, we always try to do it in automated way. It’s true for deployment of codes as well. In Sitecore project deployments, one of the key area is deploying the Sitecore packages. This can be a pain if we do it in manually (Generate Package from Sitecore environment and upload it to another environment), every time we deploy the code to another environment.

This article will help you to understand how to leverage Sitecore API to upload the TDS package.

If you build the Sitecore TDS projects in release mode, it’ll generate “*.update” packages under “bin>>Package_Release” folder. We need to upload these “*.update” files into Sitecore.

The first thing we need to do, is to expose an API/Web Service which will accept these “*.update” files and install into Sitecore. We need few Sitecore dll reference which is below.

  • Kernel.dll
  • Logging.dll
  • Update.dll

Let’s take a look at the web service code.

[WebMethod(Description = "Installs a Sitecore Update Package.")]
public void InstallPackage(byte[] file, string fileName)
{
    string path = System.Web.Hosting.HostingEnvironment.MapPath("~/tempPackages/") + fileName;
    using (MemoryStream ms = new MemoryStream(file))
    {
        using (FileStream fs = new FileStream(path, FileMode.Create))
        {
            ms.WriteTo(fs);
            ms.Close();
            fs.Close();
        }
    }

    //Upload the package
    bool hasPostAction;
    string historyPath;

    var log = Sitecore.Diagnostics.LoggerFactory.GetLogger("packageInstaller");
    using (new SecurityDisabler())
    {
        var installer = new DiffInstaller(UpgradeAction.Upgrade);
        var view = UpdateHelper.LoadMetadata(path);

        var entries = installer.InstallPackage(path, InstallMode.Install, log, out hasPostAction, out historyPath);
        installer.ExecutePostInstallationInstructions(path, historyPath, InstallMode.Install, view, log, ref entries);

        //If SITECORE 6.X
        UpdateHelper.SaveInstallationMessages(entries, historyPath);
    }
}

But make sure that the service/ API has proper security implemented (Authorization).

Now we can call this web service from anywhere (ex: Console Application, Desktop Application, Power Shell Scripts) and upload the TDS update packages. The log will be written under “packageInstaller” log.

This can be integrated in any DevOps deployment tool such as Octupus.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s