Create a scheduled job in EPiServer

This article was migrated from an older iteration of our website, and it could deviate in design and functionality.


This post explains how to create a scheduled job in EPiServer CMS.

Estimated read time : 7 minutes

Jump to

Introduction

A scheduled job in EPiServer is essentially a piece of code that will be executed repeatedly at a given time interval, or when an administrator manually executes it through the administrator interface.

What we want to accomplish

I want to have my very own scheduled job appear in the EPiServer administrator interface like so:

scheduled-job-list

In the scheduled job log I want a descriptive message explaining the result of each job execution. In this example I've created a scheduled job which counts the number of pages in the recycle bin. Each time the job is run I want it to output the number of pages in the recycle bin in the job log:

scheduled-job-log

Note that the log contains the name of the scheduled job, its description and two log entries. A new log entry is added each time the job is executed.

How to create a scheduled job in EPiServer, step by step

1) Create a new class

In order to have your new scheduled job (which is essentially a class in the EPiServer project's assembly) appear in the scheduled job list, the class needs to have the ScheduledPlugin attribute:

[EPiServer.PlugIn.ScheduledPlugIn( 
DisplayName = "Ted's scheduled job",
Description = "A scheduled job for demo purposes.",
SortIndex=1)]
public class MyPagePlugin
{
// Class logic goes here
}

Note that the ScheduledPlugin attribute is used to specify that the class is to be used as a scheduled job and it is also used to provide a name and a description for the scheduled job. I have also set the SortIndex parameter to ensure that my slick scheduled job appears at the very top of the list.

2) Implement the logic for your scheduled job

Whenever a scheduled job is triggered to be executed it looks for a static method called Execute() in the scheduled job's underlying class. The following job simply counts the number of pages in the recycle bin of the web site:

[EPiServer.PlugIn.ScheduledPlugIn( 
    DisplayName = "Ted's scheduled job", 
    Description = "A scheduled job for demo purposes.", 
    SortIndex=1)] 
public class MyPagePlugin 
{ 
    public static string Execute() 
    { 
        string resultMessage = string.Empty; 
 
        try 
        { 
            // Count the number of pages in the recycle bin 
            int pagesInRecycleBin =  
                DataFactory.Instance.GetChildren( 
                PageReference.WasteBasket).Count; 
 
            // Compose a grammatically correct (!) result message for the job history log 
            resultMessage =  
                string.Format( 
                "There {0} {1} {2} in the recycle bin.", 
                (pagesInRecycleBin==1 ? "is" : "are"), 
                pagesInRecycleBin, 
                (pagesInRecycleBin==1 ? "page" : "pages")); 
        } 
        catch 
        { 
            // Error handling goes here 
 
            resultMessage = "The job could not be completed."; 
        } 
 
        // Return the resulting message (Will be displayed in the scheduled job log) 
        return resultMessage; 
    } 
}

3) Specify how often the scheduled job should be run

Log in as an administrator. Locate your scheduled job under Scheduled Jobs on the Admin tab. Click your scheduled job to access its settings page. Here you can set a schedule for your job and also execute it manually if need be. This is also where you access the scheduled job log (by clicking the History tab):

 scheduled-job-settings

The three most important things to remember when creating a scheduled job in EPiServer

  1. Make sure you add the EPiServer.PlugIn.ScheduledPlugin attribute to your class
  2. Implement a static Execute() method in your class which contains the logic to execute when the job is run
  3. The string value returned by the Execute() method will be displayed as the message in the job history
  4. When a scheduled job is started manually it is executed in the context of the current user and its permissions. Jobs executed according to the job schedule are run in the context of an anonymous user (important when working with pages and files). See my post on how to Run a scheduled job as a specific EPiServer user.