Get last run of a scheduled job in Optimizely CMS

There could be several reasons for getting the time of last execution for a scheduled job in Optimizely CMS.

The code below first gets the job details using the Full Name (namespace + class name) without hard-coding any values. Then reads the LastExecution property. This is the same data you'll see in the Scheduled Jobs overview in the Optimizely CMS UI.

var scheduledJobRepository = ServiceLocator.Current.GetInstance<IScheduledJobRepository>();
var job = scheduledJobRepository.List()
     .Where(x => x.TypeName.Equals(typeof(MyJob).FullName))
     .FirstOrDefault();
var lastExecution = job?.LastExecution;

In your own code, use dependency injection instead of ServiceLocator, if possible.

If you need to get the last successful (or failed) execution of the job, you should look at IScheduledJobLogRepository instead.

That's it!

Found this post helpful? Help keep this blog ad-free by buying me a coffee! ☕