In this guide, you will learn how to create a basic scheduled task within Umbraco CMS. Umbraco has a built-in way of handling task scheduling. For one or two simple jobs that run every few hours, the built-in method is a great go-to tool. If you have more complex requirements:

  • You need the task to run at an exact time/date
  • You need a way to check completed without having to look within the Umbraco logs
  • The task you want to run needs to be locked down and the URL to access it cannot be public facing
  • You have a high CPU long running task

I suggest you look at using hangfire with Umbraco instead.

Enabling A Task In Configuration

If that hasn't put you off and you still want to use the built-in scheduler, in your webroot, open up 'config/umbracoSettings.config'. About halfway down the file, you will see a section called scheduledTasks, as seen below:

<scheduledTasks>
    <task log="true" alias="MyScheduledTask" interval="86400" url="/settings/myscheduledtask/" />
  </scheduledTasks>

This is the section where you can configure your scheduled tasks.
Taking the example above:

  • Log: does what you think it would do and enables logging
  • Alias: can be anything you like
  • Interval: when the task will run. This is in seconds, so 86400 = 24hours, 3600 = 1 hour
  • URL: This is the action. The web address that will be called. In this example, once every 24 hours a call will be made to settings/myscheduledtask

Scheduled Task Code

The scheduled task code is the same code you would write for any normal Umbraco controller:

  • Create a class that inherits from Umbraco.Web.Mvc.RenderMvcController
  • Create an action that does the work
  • Return an appropriate HttpStatusCodeResult

Let's see an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Umbraco.Web.Mvc;

namespace Controllers
{
    public class SettingsController : RenderMvcController
    {
        [HttpGet]
        public HttpStatusCodeResult MyScheduledTask()
        {
            // Do some work
            return new HttpStatusCodeResult(HttpStatusCode.OK);
        }
    }
}

For a quick and dirty scheduled task, it is pretty simple to set-up within Umbraco.