10 March 2014

Install .update package through an ASHX

Well, today I played a bit with TDS, adding configuration for our Internal QA environment, UAT and Prod (Authoring and Delivery). And this got me thinking on how I could install those packages automatically without having to go through the /sitecore/admin/updateinstallationwizard.aspx

So what I wanted to do is:
  • Create an ashx that will install my packages
  • Create a schedule tasks in sitecore, so I don't even have to trigger this ashx 
Awesome, let's get started

1- The Handler

So the idea is to dump the update packages on a folder. the scheduled task will watch this folder and install the packages when any... So for this exercise we will just say that the folder to watch will be:
/sitecore/admin/Automated_Packages

So let's create our ashx.

the first thing you want to do is to get the list of files on the specific folder:

            var files = Directory.GetFiles(Sitecore.MainUtil.MapPath("/sitecore/admin/Automated_Packages"), "*.update", SearchOption.AllDirectories);


The next step will be to Install the different packages when found. So inside the Foreach loop going through all the files you can execute the following code. This shows you how to use the UpdateHelper to install the package file:

        protected string Install(string package)
        {
            var log = LogManager.GetLogger("LogFileAppender");
            string result;
            using (new ShutdownGuard())
            {
                var installationInfo = new PackageInstallationInfo
                {
                    Action = UpgradeAction.Upgrade,
                    Mode = InstallMode.Install,
                    Path = package
                };
                string text = null;
                List entries = null;
                try
                {
                    entries = UpdateHelper.Install(installationInfo, log, out text);
                }
                catch (PostStepInstallerException ex)
                {
                    entries = ex.Entries;
                    text = ex.HistoryPath;
                    Sitecore.Diagnostics.Log.Error("Deployment error " + ex.StackTrace, "Automated deployment");
                    throw;
                }
                finally
                {
                    UpdateHelper.SaveInstallationMessages(entries, text);
                }

                result = text;
            }

            return result;
        }

After your packages have been installed, what you may want to do is to actually publish your changes to make sure all gets pushed to the delivery server. As our packages usually include only templates, layout, renderings, and some System items we could use something like the following to publish our content - the other way is to republish the entire site - I put both in the same method so you can choose which one is more suitable.
        protected static void Publish()
        {
            Sitecore.Context.SetActiveSite("shell");
            using (new SecurityDisabler())
            {
                DateTime publishDate = DateTime.Now;
                Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");
                Sitecore.Data.Database web = Sitecore.Configuration.Factory.GetDatabase("web");

                // publish specific section - Need to  put those in settings
                ID templateFolderID = new ID("{3C1715FE-6A13-4FCF-845F-DE308BA9741D}");
                ID layoutFolderID = new ID("{EB2E4FFD-2761-4653-B052-26A64D385227}");
                ID systemFolderID = new ID("{13D6D6C6-C50B-4BBD-B331-2B04F1A58F21}");

                PublishManager.PublishItem(master.GetItem(templateFolderID), new Database[] { web }, LanguageManager.GetLanguages(master).ToArray(),true,false);
                PublishManager.PublishItem(master.GetItem(layoutFolderID), new Database[] { web }, LanguageManager.GetLanguages(master).ToArray(), true, false);
                PublishManager.PublishItem(master.GetItem(systemFolderID), new Database[] { web }, LanguageManager.GetLanguages(master).ToArray(), true, false);
                
                // republish entire site - not sure if we want granular publish or full republish
                // PublishManager.Republish(Sitecore.Client.ContentDatabase, new Database[] { web }, LanguageManager.GetLanguages(master).ToArray(), Sitecore.Context.Language);
            }
        }


No comments:

Post a Comment