1

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 2

3

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

2

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:

  1. 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"
    }
};
  1. 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"
};
  1. 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);
  1. 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());
    }
}
  1. 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:

  1. Sitecore Scheduler: Part-1
  2. Create and Configure Scheduler in Sitecore: Part-2
  3. Pass Sitecore Items in .Net Class with Scheduler: Part-3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.