We would like to schedule a item backup(package) on specific time. Is it possible to create a package of the items while scheduling it?
2 Answers
You can try with Sitecore Powershell Extensions.
Packaging with SPE - https://doc.sitecorepowershell.com/modules/packaging
Then create a task scheduler to run this powershell script - https://doc.sitecorepowershell.com/modules/integration-points/tasks
To create Sitecore Package programmatically performs below steps which described by Hishaam Namooya in one of his articles. I am giving you the reference of that article as well:
- First step is to create a new Package Project
var packageProject = new PackageProject
{
Metadata =
{
PackageName = "Specify the package name",
Author = "Specify the author name",
Version = "Specify the version",
Publisher = "Specify the Publisher"
}
};
- Provide the package with 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"
};
- Instantiate a new Source collection and add the item source to the source collection.
//Instantiate a new SourceCollection
var sourceCollection = new SourceCollection();
//Add the Item Source to the SourceCollection
sourceCollection.Add(packageItemSource);
- Include the sub-items
//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());
}
}
- Map the directory path and generate the package.
var pathMapped = MainUtil.MapPath("Your File Path");
packageFileSource.Entries.Add(pathMapped);
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");
}
See the blog post for more detail and reference: SITECORE CREATE PACKAGE PROGRAMMATICALLY
Now regarding the schedule of the same code, you can check the series of the articles with the below links: