<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1639164799743833&amp;ev=PageView&amp;noscript=1">
Diagram Views

Using EPiServer’s Scheduled Jobs to Automate Website Tasks

Brad McDavid
#CMS, #Episerver, #Code
Published on August 25, 2015

EPiServer's scheduled jobs provide website owners and developers with robust, customizable functionality to complete automated tasks.

At Diagram, we regularly blog about the EPiServer CMS, which offers a great deal of enterprise functionality, including e-commerce, dynamic content rendering, and robust search functionality, as well as a continuous release cycle that provides regular updates to add new features and resolve errors and security issues.

Another benefit that EPiServer provides to website owners is its robust scheduled jobs functionality. This automation tool gives site owners a great deal of flexibility to schedule and run tasks at specified times of day, or even multiple times each day. Tasks don’t even have to be scheduled; they can be manually executed whenever they need to be performed.

In addition to any manually-created scheduled jobs, EPiServer offers several built in jobs, including emptying the “trash can” to free up space on the server and regular publishing and archiving of content. These tasks offer encapsulated pieces of functionality to perform certain tasks, and they are included in the core product, with no extra work needed to start using them.

In addition, EPiServer’s scheduled jobs don’t rely on external services like the Windows service to operate, which means that they allow for greater customization. Developers have a great deal of flexibility to create their own scheduled jobs to complete a variety of tasks.

At Diagram, we use scheduled jobs to import blogs from HubSpot into EPiServer. Once per day, the scheduled job contacts HubSpot, determines whether any new blogs have been created, and consumes the data. This allows for a seamless integration between systems and a simple experience when creating new website content.

Code Examples

EPiServer scheduled jobs are created in a site’s code using a class that has an attribute of EPiServer.PlugIn.ScheduledPlugInAttribute. The class must also either inherit the base class of EPiServer.BaseLibrary.Scheduling.JobBase or create an Execute() function that returns a string message to store in a log.

namespace Diagram.Example
    using EPiServer;
    using EPiServer.BaseLibrary.Scheduling;
    using EPiServer.Core;
    using EPiServer.DataAbstraction;
    using EPiServer.PlugIn;
    using EPiServer.Security;
    using EPiServer.ServiceLocation;
    using System;
    using System.Linq;
    using System.Security.Principal;
    using System.Web;
    DisplayName = "Scheduled Job Example",
    Description = "Sample scheduled job that adds text to the first child of the root page and publishes.",
    SortIndex = 0,
    DefaultEnabled = true,
    InitialTime = "1.1:0:0",
    IntervalLength = 1,
    IntervalType = ScheduledIntervalType.Days)]
    public class ExampleScheduledJob : JobBase
        private const string NameText = " - Updated by Example Job at {0}";
        private static Injected _ContentRepository { get; set; }

        public override string Execute()
            // This block allows for a scheduled job to perform content repository updates when ran on a schedule,
            // If the scheduled job is exectued manually, this block is ignored
            if (HttpContext.Current == null)
                PrincipalInfo.CurrentPrincipal = new GenericPrincipal
                    new GenericIdentity("Scheduled DummyTask"),
                    new[] { "Administrators" }

            // We use ContentReference.RootPage instead of ContentReference.StartPage since the job may run in the background without HttpContext set
            // the GetChildren can be used to filter to a specefic page model.
            var firstChild = _ContentRepository.Service.

            if (firstChild == null)
                return "Failed to retrieve first child of the root page";
            // Create clone for modification             firstChild = firstChild.CreateWritableClone() as PageData;             string[] oldName = firstChild.Name.                 Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);             firstChild.Name = oldName[0].TrimEnd() +                 string.Format(NameText, DateTime.Now.ToString("yyyyMMdd"));             _ContentRepository.Service                 .Save(firstChild, EPiServer.DataAccess.SaveAction.Publish);             return "Example Job Completed";         }     } }

Mathias Kunto also has a great open source project on GitHub that allows settings to be created for scheduled jobs.

One thing to remember when creating scheduled jobs is that if the job will be changing the site’s content, a user needs to be associated with the change. The following code snippet can be used to “spoof” a user account and allow a scheduled job to make this type of change:

if (HttpContext.Current == null)
	PrincipalInfo.CurrentPrincipal = new GenericPrincipal
		new GenericIdentity("Scheduled DummyTask"),
		new[] { "Administrators" }

EPiServer’s scheduled jobs are just one of the many great tools that this CMS platform provides, and when used in conjunction with other resources available to developers, they can extend a site’s functionality to do nearly anything imaginable. Do you have any questions about how to implement scheduled jobs on your EPiServer website? Do you want to know more about the customization tools that EPiServer provides to site owners? Please contact us to speak with a Solutions Engineer, or feel free to share any customization tips of your own in the comments below. We’d love to hear from you!

Image Credit: Clock vector designed by Freepik