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");
}}
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:
No need to specify the header.
First Column:
Provide the Sitecore Path
Second Column:
Specify if it is Item or File
Third Column:
Specify if you will Include Subitems
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:
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" />