Creating a Scheduled Task in Sitecore

Hey there, Sitecore developers! Ever wished you could automate those repetitive tasks that eat up your time? Well, you’re in luck. This short tutorial is here to show you how to create scheduled tasks in Sitecore.

Whether it’s sending out reminder emails or publishing content at specific times, setting up these tasks can make your life a lot easier. Let’s dive in and see how you can get your Sitecore environment running like a well-oiled machine with minimal effort.

General steps

  1. Write the Task Code.
  2. Create a Command.
  3. Set up the Scheduler

With that in mind, let's get started.

1. Write the Task Code

The first step in creating a scheduled task in Sitecore is to write the code that will be executed. This involves creating a class with a method that contains the logic you want to run. For example, we can have a class named ScheduledJob with a method called Execute.

Here’s a simple example.

using System;
using Sitecore.Diagnostics;
namespace YourNamespace
{
    public class ScheduledJob
    {
        public void Execute()
        {
            try
            {
                // Your logic here
                Log.Info("From the scheduled task in Sitecore.", this);
            }
            catch (Exception ex)
            {
                Log.Error("Oops, something went wrong in the task.", ex, this);
            }
        }
    }
}

2. Create a Command in Sitecore

Once you have your task code ready, the next step is to create a command in Sitecore. This command will reference the class and method we created earlier, allowing Sitecore to execute it on schedule.

To create and manage the command, we will need the following.

  1. In Sitecore, go to /sitecore/system/tasks/commands.
  2. Create a New Command Item, such as a Scheduled Job Command.
  3. Configure the Command, referencing the class and method created previously.

The command item could look like this.

Command item

3. Set Up the Scheduler in Sitecore

After creating the command, the next step is to set up the scheduler in Sitecore. This scheduler will determine when and how often the task runs.

To set up the scheduler item, we will need the following.

  1. In Sitecore, go to /sitecore/system/tasks/schedules.
  2. Create a New Scheduler Item, for example, a Scheduled Job.
  3. Configure the Scheduler.

The scheduler item could look like this.

Scheduler item

The fields in this item have the following objectives.

  • Command: command created in the previous step.
  • Schedule: Define the interval at which the task should run (e.g., daily, weekly).
  • Items: Specify any Sitecore items if needed.
  • Async: Check this box if the task should run asynchronously.
  • Auto Remove: Use this field to automatically remove the schedule definition when the task expires.

For the schedule format, this value example 20231125|99990101|127|12.00:00 means.

  • Start Date: November 25, 2023 (20231125)
  • End Date: Never ends (99990101)
  • Days of the Week: Sunday, Monday, and Saturday (127)
  • Time: 12:00 PM (noon) (12.00:00)

With all this, the task can now be executed automatically as scheduled or manually in the case of tests.

Task Schedule

Thanks for reading!

Automating tasks in Sitecore can make your life a lot easier. By writing the task code, creating a command, and setting up a scheduler, we can streamline operations and free up time for more important things.

If you have any questions or ideas in mind, it'll be a pleasure to be able to communicate with you and exchange knowledge with each other.

Happy scheduling!


Recommended Free Ebook
Similar Articles