20 April 2014

Serialization of Content Tree prior installing packages


One of my colleague asked me a question about how could you get the data from a server and restore it into his local machine. Obviously I told him take a DB backup...Dough.... Well the issue was that he did not have access to the DBs so needed to request a backup from the Dba, not convenient. So I was starting on with Serialization. I show him how to serialise items from the tree and deserialise them on his local. I kind of love those discussion as it always start on a topic then extend to something more useful. In this case we ended up on: could we serialise the content tree as backup before installing any packages.

So what we were thinking about was:

1- Serialising the tree: well that is the easy bit - for this code sample I will take the entire tree:

        string SitecoreRootItem = "{11111111-1111-1111-1111-111111111111}";

        protected void Page_Load(object sender, EventArgs e)
        {
            BackupItemTree(SitecoreRootItem);
        }

        public void BackupItemTree(string id)
        {
            Database db = Sitecore.Configuration.Factory.GetDatabase("master");
            Item itm = db.GetItem(id);

            Sitecore.Data.Serialization.Manager.DumpTree(itm);
        }

2- Events.. Events... Events
Now I am sure you are familiar with Events in sitecore, well guess what there is an event for package installation:
      < event name="packageinstall:starting" />
      < event name="packageinstall:items:starting" />
      < event name="packageinstall:items:ended" />
      < event name="packageinstall:files:starting" />
      < event name="packageinstall:files:ended" />
      < event name="packageinstall:ended" />

So easy to hook up our code on the packageinstall:starting...
configuration:
      < event name="packageinstall:starting">
        < handler method="OnPackageInstall" type="MyProject.Business.Sitecore.Events.PackageInstall.PackageInstallEventHandler, MyProject.Business">
      < /handler>
< /event>

for the Handler:
using Sitecore.Data;
using Sitecore.Data.Items;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SC = Sitecore;

namespace MyProject.Business.Sitecore.Events.PackageInstall
{
    public class PackageInstallEventHandler
    {
        protected void OnPackageInstall(object sender, EventArgs args)
        {
            string SitecoreRootItem = "{11111111-1111-1111-1111-111111111111}";
            BackupItemTree(SitecoreRootItem);
        }

        private void BackupItemTree(string id)
        {
            Database db = SC.Configuration.Factory.GetDatabase("master");
            Item itm = db.GetItem(id);

            SC.Data.Serialization.Manager.DumpTree(itm);
        }
    }
}

Now if we clear the serialisation foder:


Now let's install a folder:


Now let's check the serialisation folder again:



Now you have a backup automatically before installing packages... Please note that this was just to show what you could do with event before installing packages... Obviously, if your content tree is really big, you could have some timing out issues...

No comments:

Post a Comment