Update SharePoint Timer Job’s progress bar – the easy way

Every time I develop a timer job, I always add a progress bar. Well, if the job runs long enough that is. It adds that bit more refinement to the whole project and it’s pleasant to see your job crunching through the numbers.

For this example I’ve set up a standard timer job project:

timer job project structure

  • A Web Application scoped feature which installs the timer job
  • A static class ProgressUpdater which will handle the timer job’s progress updating
  • A class service which will do something
  • A class TimerJob which is our core job definition

I’ve chosen to add a class service to demonstrate the update process from outside the JobDefinition scope.

For the timer job I have the following code:

using Microsoft.SharePoint.Administration;
using System;

namespace SharePointTimerJob
{
    public class TimerJob : SPJobDefinition
    {
        private const string JOBNAME = "Custom Timer Job";

        public TimerJob()
            : base()
        {
        }

        public TimerJob(SPWebApplication webApplication)

            : base(JOBNAME, webApplication, null, SPJobLockType.Job)
        {
            this.Title = JOBNAME;
        }

        public TimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)

            : base(jobName, service, server, targetType)
        {
        }

        public static string JobName
        {
            get { return JOBNAME; }
        }

        public override void Execute(Guid contentDbId)
        {
            ProgressUpdater.Job = this;

            var service = new Service();
            service.Process();
        }
    }
}

As you can see it’s pretty straight forward. One note here is the ProgressUpdater.Job = this; line. This sets the Job property of the ProgressUpdater class to the executing instance.

The next piece of code is the ProgressUpdater:

namespace SharePointTimerJob
{
    public static class ProgressUpdater
    {
        public static TimerJob Job
        {
            get;
            set;
        }

        public static void Update(int percentage)
        {
            if (Job == null)
            {
                // throw or return
                return;
            }

            if (percentage >= 0 && percentage <= 100)
                Job.UpdateProgress(percentage);
        }
    }
}

And the code of the service to trigger an Update call:

using System.Threading;

namespace SharePointTimerJob
{
    public class Service
    {
        public void Process()
        {
            int[] collection = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int count = 0;
            foreach (var item in collection)
            {
                Thread.Sleep(5000);
                count++;
                ProgressUpdater.Update((count * 100) / collection.Length);
            }
        }
    }
}

All this does is sleep the thread and update afterwards, but you get the idea.

When you run the timer job you’ll see that the progress updates nicely in ticks of 10% because of the collection, which is 10 items:

progress 10

progress 40

2 thoughts on “Update SharePoint Timer Job’s progress bar – the easy way

  1. progress bar is working fine . but it is not align with process. it just update based on sleep time time . it has to be more dynamic based on item status it should be progress.

Leave a reply to Rahul Barua Cancel reply