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;
myCustomTimerJob.Update();
=============================================
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
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.
Thanks
Sam
Hi,
Are you using SharePoint 2010 environment? There could be issue of “Allow Remote administration”. Often I just switch this setting as permanently on in dev, but sometimes I’ll forget it when going to deployment. Althought there are a few good powershell scripts to do the trick. More about this “guess” in here: http://www.sharepointblues.com/2010/10/22/custom-spjobdefinition-and-access-denied-error/
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
Yes I am using Sharepoint 2010 envirioment
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.
http://www.directsharepoint.com/2011/04/access-denied-while-activating-feature.html
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.
Pingback: Start content type synchronisation from within a feature « Vojtech Nadvornik's Blog