Creating a Scheduled Task in Sitecore

esdanielgomez

Daniel Gomez

Posted on November 26, 2024

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);
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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:

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

The command item could look like this:

command values

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:

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

The scheduler item could look like this:

Scheduler item fields

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)

And with all this, the task can now be executed automatically as scheduled, or manually as well in the case of tests:

Run Task

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 be in communication with you, and together exchange knowledge with each other.

X / LinkedIn - esdanielgomez.com

Happy scheduling!

💖 💪 🙅 🚩
esdanielgomez
Daniel Gomez

Posted on November 26, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Creating a Scheduled Task in Sitecore
dotnet Creating a Scheduled Task in Sitecore

November 26, 2024