Setting up a Scheduled Task Is Easier Than You Think!

Now there's more than one way to create a scheduled task, but I prefer using a configuration file rather than setting one up in /sitecore/system/Tasks/Schedules. Why? Well it's simpler to set a configuration file, and you know someone isn't going to change it in the Content Editor, if that's what you're looking for (we've had clients with admin rights change templates, etc). 

Using Configuration Transforms and proper Build Configurations will also allow you to set different frequency occurrences depending on environment (Dev, QA, UAT, Prod, etc). Since the release of Sitecore 9 you actually have a choice of using the much improved Rule based configuration instead of Configuration Transforms, so an agent is added for a specific environment with a certain frequency.

What you see here is a simple task that would clean duplicates. What duplicates? Well that's up to you since you can write any code for any purpose here, but just be mindful of the frequency and how intensive it is. Task methods do not accept parameters, so we use properties instead, like the StorageRoot example. The task is run by an agent, and it will look to execute Run(). 

namespace Sitecore.Feature.Forms.Tasks
    public class CleanDuplicates
        public string StorageRoot{ get; set; }
        public void Run()
            Log.Info($"[{typeof(ExecuteRequest).FullName}.{nameof(Run)}] -> Task running with root of {StorageRoot}", this);

How does this get executed? The initialize pipeline includes a processor called InitializeScheduler, which checks for any agents that require running. The frequency of this check is specified in /configuration/sitecore/scheduling and the default is 5 seconds. So, with the following file, Sitecore would check every 5 seconds to see if an agent requires running, and this one would do so every 30 minutes.

<configuration xmlns:patch="">
    <sitecore role:require="ContentManagement or Standalone">
            <agent type="Sitecore.Feature.Forms.Tasks.CleanDuplicates, Sitecore.Feature.Forms" method="Run" interval="00:30:00">

Creating a Scheduled Task in Sitecore

If you'd still like to make a scheduled task without using a config, just follow these steps. 

Make a new command under system -> tasks -> commands.

  1. Under Type field, enter the class and assembly name.
  2. User method, well, enter the method.

Make a new schedule under system -> tasks -> schedules.

  1. Choose the command you just made.
  2. The schedule field can follow this format, which would run every 10 minutes: 20160101T235900|20250101T235900|127|00:10:00