In this tutorial, you will learn how to set up a scheduled task in Episerver CMS. If you need to enable a reoccurring task in Episerver then creating a custom scheduled/task is the way to go. An Episerver scheduled task will allow you to run some code periodically. A scheduled task can be set to run in any time interval that makes you happy, be it daily, weekly, monthly etc... Scheduled tasks are great for sending emails, scraping content, pulling in products, updating pricing, clearing database logs, the list of useful possibilities is near infinite. Luckily, creating a scheduled task is pretty straightforward. Follow these tasks:

  • Create a new class called whatever you want and inherit from JobBase
  • Decorate the class with the ScheduledPlugIn attribute and add a display name
  • Override a public static method called Execute(). This is the method that will be called when the scheduled job is run
  • Override the ToString() method to customise the message is that returned to a content editor after the scheduled job completes

Sounds easy enough, let us look at some code:

After you have created your job, compile and run your site. You can now test your job by manually triggering the task. You can do this by going into the Episerver admin UI. Your task should appear in the Scheduled Tasks area. Find your task, click on it and then click on the start button, as seen in the image:


If everything has gone to plan, your tasks should run successfully. Unfortunately, sometimes your task will fail due to an error. Reasons for an error occurring are again near-infinite, however, a common issue is a failure due to HttpContext being null. So what's going on here 🤔

Why is the HttpContext null inside my scheduled task?

In an Episerver architecture, scheduled tasks are run independently in their own windows service. This means that all scheduled tasks run outside of the normal page life-cycle. When a task runs it will not have access to an instantiated HttpContext. As HttpContext only gets created for normal page requests, it will be null.

One bit of data that I frequently need to use within a task is access to the folder location of the application on the server. Getting this from HttpContext is usually my go-to, however, we need an alternative approach in a scheduled task. Luckily, Microsoft has provided a non HttpContext dependant helper to get a lot of data about the current process. If you need a value like the ApplicationPhysicalPath you can use HostingEnvironment which is found in System.Web.Hosting, like so:

That covers the basics of getting started building your own Episerver scheduled task. To re-cap, you need to inherit from JobBase and implement the required fields. When building your first scheduled task I also recommend reading How To Debug An Episerver Scheduled Task Or InitializationModule to learn how to attach a debugger. This can save you a lot of wasted time!

As always a fully working code sample can be downloaded from my Github page here. What scheduled tasks have you worked on before? Leave your examples in the comments section below. Happy Coding 🤘