Starting custom timer job programmatically

Okay Tech Days 2009 Finland is over and I managed to get a few good tips and tricks. I must thank Juhani Lith (Microsoft) for providing some information regarding the question about where to place code in SharePoint 2007 that takes a lot server resources temporarily. Currently the best choice is to put that code into Custom Timer Job. SharePoint Timer Jobs are excellent into this kind of function since I do not need to write own code for a service to do this asynchronously. This solution needs of course some serious testing and as Juhani mentioned, it might cause other timer jobs to fail or work poorly when this other code runs. And a side note: when I was sitting in the train headed to Tech Days 2009 (journey started at 3 a.m. 😉 I actually wrote beginning code for this Timer Job without knowing was this right or not. There is also another good place for this code and it is Workflow (w3wp.exe). We’ll see which one is the better way…

And now for the stuff that topic is all about: Starting custom timer job programmatically (or by code, all about right words). Let’s start from the point where you have done a Custom Timer Job (there’s now plenty of stuff from this topic). And now you want to start it programmatically, on demand. This topic is not so much talked about but after a trial and error I found a way to do this from code.

The code:
Web Part side (like a button event or so, where you want to start the Timer Job):

SPSite site = SPContext.Current.Site;

string timerJobName = "HBExportJob";

CustomTimerJob myCustomTimerJob= new CustomTimerJob (timerJobName, site.WebApplication);

SPOneTimeSchedule schedule = new SPOneTimeSchedule(DateTime.Now);

myCustomTimerJob.Schedule = schedule;

CustomTimerJob is a class that is inheritated from SPJobDefinition.
That code kicks the timer job running programmatically. I began this process by creating a custom timer job and then I looked about the common installer classes from examples that are in the Internet. Then it hit like a ligthning from a clear sky, you can bypass that installer and put actual logic class with your webpart and start that timer job. Also you could iterate through all SPJobDefinitions and find your Timer Job in that SPWebApplication.JobDefinitions and start from there programmatically.

I hope that someone else gets a bright ideas like this. Now when I look at this I can say that this is not much but when you go all the way through the process of thinking, trying and failing and trying again, the feeling is something very different.. Btw I also had a code that actually went through SPWebApplication.JobDefinitions and added the timer job deep in there and executed the timer job but it led me having synchronous timer job with web part. Not nice in this case but possible 😉

Okay btw. I am sorry for the baaad english 😉


About Sakarih

I am SharePoint developer/architect from Finland and my blogposts are all about SharePoint 2007/2010/2013 stuff. There might be also something else posted.
This entry was posted in Uncategorized. Bookmark the permalink.

7 Responses to Starting custom timer job programmatically

  1. Sam says:

    Hi ,
    Thank you for the wonderful post.
    I am facing a problem when creating thetimer Job webpart.I did created a custom webpart that can view and execute and disable the timer jobs in my development server but when I tried to deploy in the Beta envirionment the webpart failed as it can display only the timer job status but it cannot execute or update as its throwing me access denied error. Note:In both the enviriomnet when you run as an elevated privilage its using the service account which is farm administrator for both with having the permission dbowner in Config database.

  2. Sam says:

    Thank you for the prompt
    I can run the custom timer job with feature in both of the envirionment.But Now I have a requirement where I need to create a webpart which has an abilty to start a timer job without getting to central admin.SO I created a webpart which shows the details of the timer Job and it has some submit button on click of which it can run,schedule ,disable a custom timer Job.I implemented in dev box it working perfect without any problem but I cannot run a timer job from webpart though it shows the timer job history .Note:In both envirionment its using elevated privilage which uses a service account which is a farm administrator with dbowner in Config database.
    Thanks Sam

  3. Sam says:

    Thank you for the help.Since I am not authorised to run any powerscript in server I wrote a piece of code in the webpart feature to set remoteadministratoraccessdenied=false.So It worked like a charm for me.Hope this might help anybodyelse.

    • sakarih says:

      It’s good to hear that you were able to solve the problem. Sometimes the testing & deployment phases can surprise by having some of the internal properties different. This remote administrator access denied feature was partically nasty since it was totally new feature in SharePoint 2007.

  4. Pingback: Start content type synchronisation from within a feature « Vojtech Nadvornik's Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s