Sitecore Create Package Programmatically

The creation of Sitecore Package is easily performed using the Package Designer UI in Sitecore. The UI is very user friendly. The Sitecore Package allows the “transfer” of items from one environment to another or is used to backup specific items.

sitecore-package

But what about creating package through code. In this post, I will explain how to achieve the creation of Sitecore Package programmatically. Using my favorite decompiler tool, I had a look in the Sitecore Kernel to see how the generation of package is being performed.

First step is to create a new Package Project as shown below:

var packageProject = new PackageProject
{
    Metadata =
    {
        PackageName = "Specify the package name",
        Author = "Specify the author name",
        Version = "Specify the version",
        Publisher = "Specify the Publisher"
    }
};

These are the basic properties that is also available when creating the package from the Package Designer. Once this is done, we need to provide the package the source of the items or files.

//Creating Source for Files
var packageFileSource = new ExplicitFileSource
{
    Name = "File Source Name"
};

//Creating Source for Items
var packageItemSource = new ExplicitItemSource
{
    Name = "Item Source Name"
};

For files, it is a direct process. You only need to instantiate a new source then add the different path to the newly created source. However, for items, it is slightly different. You may add a single item or with its subitems. In order to be able to add the Parent Item with its children, you will need to create a Source Collection. It is the Source Collection which is responsible for the storing the Parent with its children.

//Instantiate a new SourceCollection
var sourceCollection = new SourceCollection();

//Add the Item Source to the SourceCollection
sourceCollection.Add(packageItemSource);

If the item to be added should include subitems, we will add it to the source collection else it will added to the item source as shown below:

//Include subitem. You may have a method to set the include subitem.
var includeSubItem = "true / false";

var itemUri = Factory.GetDatabase("master).Items.GetItem("path to item");

if (itemUri != null)
{
    if (includeSubItem.Equals("true"))
    {
        sourceCollection.Add(new ItemSource()
        {
            SkipVersions = true,
            Database = itemUri.Uri.DatabaseName,
            Root = itemUri.Uri.ItemID.ToString()
        });
    }
    else
    {
        packageItemSource.Entries.Add(new ItemReference(itemUri.Uri, false).ToString());
    }
}

For files, you will need to map the directory path before adding it to the File Source.

var pathMapped = MainUtil.MapPath("Your File Path");

packageFileSource.Entries.Add(pathMapped);

Once the different items or files have been added to the respective sources, we need to add these sources to the package project, save the project and finally, generate the package.

if (packageFileSource.Entries.Count > 0)
{
    packageProject.Sources.Add(packageFileSource);
}

if (packageItemSource.Entries.Count > 0 || sourceCollection.Sources.Count > 0)
{
    packageProject.Sources.Add(sourceCollection);
}

packageProject.SaveProject = true;

using (var writer = new PackageWriter(MainUtil.MapPath("Path to save package")))
{
    Context.SetActiveSite("shell");

    writer.Initialize(Installer.CreateInstallationContext());

    PackageGenerator.GeneratePackage(packageProject, writer);

    Context.SetActiveSite("website");
}

I have already implemented a module whereby you upload a csv file. The package will be generated with items or files provided in the uploaded csv file. The module has already been submitted to Sitecore Marketplace and is in reviewing mode. I will update this post with the link once the module is published. You can still check my GitHub Repo to have a look and modify the code to fit your requirements.

Advertisements

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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s