Reading Update Package – Extracting Commands

When working with huge and complex content architectures, we often need to transfer content between different staging environments.

We usually use Sitecore packages for this, but with large content sets, it becomes an expensive operation and the resulting package sizes can be huge, because they contain all of an item’s content, not just the things that change.

Though update package is a perfect solution for this, comprising of only the changes/commands i.e. fields/versions/items, we might need to understand creating and also reading an update package which will help when any customization is required.

This blog helps you to read an update package, which will greatly help when recurring customization/manipulation is required at the target database before or after installation like, initiating a backup before installation, tracking the commands and changes, customizations to default publish/workflow operations, automatically copy selected content from one language to another etc.

We know we have two types of Commands,

  1.    Item Commands
  2.    File Commands

Commands can be retrieved from an Update package using ZipReader API.

We have the package.zip entry inside UPDATE Package file and we have the item command folders inside package.zip.

 

XML files inside the command folders will be retrieved using ZipArchive API. We can extract the Commands from the XML using DeserializeCommand method of SerializationCommandFactory class.

ZipReader updatePackageReader = new ZipReader(fileName, Encoding.UTF8);
ZipEntry packageEntry = updatePackageReader.GetEntry("package.zip");

//Retrieving the entries inside Package.Zip file
using (ZipArchive zipArchive = new ZipArchive(packageEntry.GetStream()))
{
   foreach (ZipArchiveEntry zipEntry in zipArchive.Entries)
   {
      if(zipEntry.FullName.Substring(0, zipEntry.FullName.IndexOf('/')).Contains(Sitecore.Install.Constants.ItemsPrefix))
      {
         //Reading the Command XML
         XmlReader reader = SerializationUtil.GetReader(zipEntry.Open());
         reader.MoveToContent();

         //Retrieving the Command(s) from Command XML
         ICommand command = SerializationCommandFactory.DeserializeCommand(reader, SerializationCommandFactory.GetSerializationContext());
         ExecuteItemCommand(command);
      }
   }
}

For More Details on Executing Commands read the following blogs,

Implementing Item Commands

Implementing Version Commands

Implementing Field Commands

Originally Posted at:  https://www.nttdatasitecore.com/Blog/2016/December/Reading-Update-Package-Extracting-Commands

Leave a Reply

Your email address will not be published. Required fields are marked *