allBlogsList

Sitecore and PowerShell

Create Anti-Package, Install the Package and Publish the items listed in the package - Using Sitecore PowerShell Extensions

First of all, I would like to begin by congratulating Adam Najmanowicz, Michael West and others who contributed in delivering an amazing addition to the Sitecore community - [Sitecore PowerShell Extensions](https://marketplace.sitecore.net/en/Modules/Sitecore_PowerShell_console.aspx "Sitecore PowerShell Extensions")

There are a lot of tools in market which address Sitecore Package migration to different environments. Sitecore PowerShell Extensions is one of the modules which can be customized as per your requirements.

Let's consider the following scenario:

  • For every deployment, each developer packages their work done in Sitecore using Sitecore Package Designer.
  • They build a *.zip file with all the items that need to be migrated and also a supporting xml file which has information on contents of the package.
  • Each package needs to be installed, followed by publishing the items which are defined in the package.
  • Full site publish is not an option after installation of packages.

This is a time consuming process for every deployment and needs to be automated. After extensive research, I have decided to install Sitecore PowerShell Extensions-3.3 for Sitecore 7 module.

I started with creating Sitecore zip and xml package files using Sitecore Package Designer and saved them in a shared location (Example: DataFolder\Packages\ReleasePackages).

This shared location can be specific to your requirement.

Sitecore Release Packages Location

What is an Anti-Package and why do we need it?

Anti-Package is used as backup strategy. This is beneficial and best practice to make use of them, which allows you to rollback to the stable version of the item(s) in the Target environment before Package Installation. All Anti-Packages generated using this script will be stored under $rollBackLocation.

$SitecorePackageFolder is the default packages location for any Sitecore Instance located under the Data Folder. This value is pre-defined as part of the module.

1. Create folder(s) for each deployment to maintain a clean structure

a. These folders will be created under ReleasePackages

b. $location will be created every deployment

c. $completedLocation folder stores all the packages post-installation.

d. $rollBackLocation folder stores all the Anti-Packages pre-installation.

e. This way, we can keep track of the list of packages for each deployment. 

SPECreateFolders

2. Fetch the zip and xml files from the shared location

a. Make sure each zip package and its associated xml file names match

SPEFetchPackages

3. Create Anti-Package followed by Sitecore Package Installation for each zip file in your shared location

a. We are fetching the item ID's that are part of the package by reviewing the xml files associated with each zip package.

b.Only existing items in target environment will be used for creating Anti-Package.

c. Newly added items will be ignored since they do not exist in target environment.

SPECreateAntiPackage_1

4. Once we get the items that are valid, they will be exported as a Anti-Package

a. They are stored in the $rollBackLocation in case we have to roll back the deployment.

b. The generated Anti-Packages will be using a unique naming convention which helps us understand the order in which we can perform rollback. 

SPECreateAntiPackage_2

5. Installing Sitecore Package

a. These installed packages will be renamed accordingly to know the order in which they were installed..

b. All Installed Packages will be moved to the $completedLocation.

SPEInstallPackage

6. Publishing each of the contents of the Sitecore Package

a. Get the list of item ID's that need to be published by reviewing each xml file associated with the zip package.

b. Once the publishing is complete for a package, it is moved to $location.

SPEPublishItems_1

SPEPublishItems_2

This entire script will be added to the PowerShell script library and can be executed either using PowerShell ISE or adding it as part of the PowerShell ToolBox.

SPEToolBox

By following these steps, we are reducing manual effort of installing and publishing the content items of a package. We can now execute this by one click of a button.

This script has been developed and tested on Sitecore.NET 7.2 (rev. 140228). Please click here to download the script file in plain text which can be added to your script library.

Stay tuned for Part II in which I will discuss about handling this script to run outside of Sitecore. 

Disclaimer: Sitecore PowerShell Extensions is a powerful tool. Always make sure it is thoroughly tested before applying it on Production environment.