Winsmarts.com

Microsoft MVP

MVP Logo

Awarded the Microsoft MVP Award.

Hosted By

blah!bLaH!BLOG!!

Developer tip - Testing SharePoint timer jobs in a development environment

Posted on 6/24/2007 @ 11:45 PM in #Sharepoint | 5 comments | 6664 views

Okay, I've been so goddamn busy, that it sucks that I have so much to blog about, and no time to do so.

Anyway, here is something that I know everyone will find useful at some point or the other.

SharePoint 2007 has a number of timer jobs. These are things that run on a scheduled basis, you can go check out a list of them at

Central Administration > Operations > Timer Job Definitions

So say for instance, I find myself with a client who wants me to deliver a solution based on "Information Management Policy" in a flat 4 hours. Well thats great, except I can't demo that solution because guess what - That job is set to run on a daily basis. I think it would be hella useful to test such things that run based on a timer in a development environment anyway, so there has to be a way to configure these jobs to run on a shorter time schedule.

Luckily, there is such a way.

For instance, the "Information Management Policy" job, is a part of the "PolicyConfigService". So I can use the SharePointAPI to write code as shown below -

static void Main(string[] args)

{

    using (SPSite site = new SPSite("http://moss2007"))

    {

        SPServiceCollection services = site.WebApplication.Farm.Services;

 

        foreach (SPService service in services)

        {

            if (service.Name == "PolicyConfigService")

            {

                foreach (SPJobDefinition job in service.JobDefinitions)

                {

                    if (job.Title == "Information management policy")

                    {

                        SPMinuteSchedule schedule = new SPMinuteSchedule();

                        schedule.BeginSecond = 0;

                        schedule.EndSecond = 59;

                        schedule.Interval = 5;

                        job.Schedule = schedule;

 

                        job.Update();

                    }

                    Console.WriteLine("JOB: " + job.Title);

                }

            }

        }

    }

}

There you go.

As you can see, I sneakily replaced the schedule for the Information management policy job to a minute based schedule.

Now, when I set a site collection policy to delete a document, I can demonstrate that working in a minute's time.

Purty darned useful I say :).


On 7/25/2007 1:10:28 PM Yongbo said ..
I am wondering how can we save the Interval value somewhere, so make it configurable. During the timer job is in running, we are able to change how aften the timer job fires. Otherwise we have to change, compile, and deploy again, if we need to change the interval, say, from 5 minutes to 60 minutes.


On 5/27/2008 3:03:28 PM MiHo said ..
Or you can just use the STSADM command

http://technet.microsoft.com/en-us/library/cc262865.aspx


On 6/24/2008 3:32:00 PM Lester said ..
This is very cool. How about with Wss rather than MOSS? The PolicyConfigService is not a running service on a wss box as far as I can tell. The link posted by miho might work but would seem to be not granular enough really.


On 6/24/2008 3:55:56 PM Lester said ..
The codeproject solution here does the trick for wss. http://www.codeproject.com/KB/sharepoint/stsadm_commands.aspx in case anyone else needs it.


On 6/24/2008 8:57:54 PM Christopher Scolt said ..
Rather than modifying the schedule of your timer job, you can run it as a once off by calling job.Execute();

Please post your comments:


Your feedback will be submitted for moderation, and will appear after it is approved.

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Site designed and maintained by Sahil Malik | All Rights Reserved. ©2007 WinSmarts.com.