sitecoresitecore8sitecore8.1

How to add sitecore items to a sitecore package programatically using Sitecore.Install.PackageGenerator?


I want to programatically create package of all items in my sitedata folder using Sitecore.Install.PackageGenerator . I am able to add Uri property of each item to package but cannot figure how to add the item itself. Please find below my code snippet.

using (new Sitecore.SecurityModel.SecurityDisabler())
        {
            Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("web");
            Sitecore.Install.PackageProject document = new Sitecore.Install.PackageProject();

            document.Metadata.PackageName = "SitecoreBackup"+DateTime.Now;
            document.Metadata.Author = "PrePublishEvent";

            Sitecore.Install.Items.ExplicitItemSource source = new Sitecore.Install.Items.ExplicitItemSource(); //Create source – source should be based on BaseSource  

            source.Name = "SitecoreBackupSource";
            Sitecore.Data.Items.Item[] items = db.Items.Database.SelectItems("/sitecore/content/Sites/mysite/Site Data/*");

            foreach (Sitecore.Data.Items.Item item in items)
            {
               // source.Entries.Insert(0,item.ToString);
                source.Entries.Add(new Sitecore.Install.Items.ItemReference(item.Uri, false).ToString());
            }

            document.Sources.Add(source);
            document.SaveProject = true;

            //path where the zip file package is saved  
            using (Sitecore.Install.Zip.PackageWriter writer = new Sitecore.Install.Zip.PackageWriter("/data/packages/" + "SitecoreBackup" + DateTime.Now.Ticks.ToString() + ".zip"))
            {
                Sitecore.Context.SetActiveSite("shell");
                writer.Initialize(Sitecore.Install.Installer.CreateInstallationContext());
                Sitecore.Install.PackageGenerator.GeneratePackage(document, writer);
                Sitecore.Context.SetActiveSite("website");
            }}

Solution

  • I have implemented a tool which perform exactly what you required. It is still in Reviewing Mode on Sitecore Marketplace. I will update the answer once it is approved.

    But in the meantime, you can download it here and the code is available on my GitHub. Below is how to use the tool: enter image description here

    1. Install the Sitecore Package through the Installation Wizard.
    2. Open Sitecore Menu and click on Package Auto Generator.
    3. Upload a CSV file. The CSV file should be in the following format:

    enter image description here

    No need to specify the header.

    Once uploaded, fill in the necessary field on the application and click on Next. The package will be generated and can be downloaded. For your case, you can have the CSV file as shown below:

    enter image description here

    Also, you will need to change the config file that is accompanied with it to specify your Source Database. As for your case, it will be set to web. The config file is found in the App_Config/Include/Sitecore.PackageAutoGenerator.Settings.config and change

    <setting name="SourceDatabase" value="master" />
    

    to

    <setting name="SourceDatabase" value="web" />