Visual Studio 2005 Project Template for SharePoint 2007 Timer Jobs
What are SharePoint Timer Jobs?
SharePoint timer jobs are tasks executed on a scheduled basis by the SharePoint Timer service.
SharePoint comes by default with a lot of timer jobs, like profile synchronisation between Active Directory and SharePoint. To see all currently installed timer jobs go to Central Administration > Operations > Timer Job Definitions.
Developing your own SharePoint Timer Job
To create your own SharePoint 2007 timer job you have to perform the following steps:
- Create a class library project.
- Create a class derrived from SPJobDefinition that contains the business logic.
- Create a class derrived from SPFeatureReceiver that can be deployed and functions as an installer for the timer job.
- Create a feature description file: feature.xml
- Create a solution description file: manifest.xml
- Create build description files for makecab.exe to build a .wsp (cab) file: .ddf and .Targets
- Build the wsp file.
- Add the solution to SharePoint.
- Deploy the solution.
- Activate the feature.
Nobody wants to do all these steps for each new timer job. So in order to make it simpler to start a new timer job project, I created a visual studio template containing all the neccessary stuff you need.
Visual Studio 2005 Project Template
Andrew Connell wrote a good article on his blog on how to create a SharePoint timer job and to modify the visual studio project to get a deployable .wsp file after building the project. Based on his article I created a reusable visual studio template. In addition I included a setup.bat file that can deploy and undeploy the .wsp file.
How to use the template
- Download the template and save it in MyDocuments\Visual Studio 2005\Templates\Visual C#.
- Create a new project using the “Custom SharePoint Job” template (Be careful to not use a dot in the project name).
- Write your logic inside the Execute method.
- Build the project.
- Run the setup.bat in your project folder to install, deploy and activate your job (Do not copy it into your bin folder, just run it. Everything is taken care for you.
).
If no errors are shown your timer job is listed in the Timer Job Definition list of the central administration. Navigating to Timer job status you can find out if your job was executed and if there were any errors. In case of an error exception messages are written to the EventLog by SharePoint.
Tip: Before you start customizing your project (namespace, assembly version, file names) look inside every file to get an overview on how everything fits together.
Good luck on making good (timer-)jobs,
Alexander Brütt
Download: CustomSharePointJob.zip
Deploying the timer on a SharePoint Farm:
I had problems deploying the timer job on a farm. The following commands deployed the timer on the farm correctly:
1. Add the solution
stsadm -o addsolution -filename {WSPFILENAME}
2. Deploy the solution
stsadm -o deploysolution -name {WSPFILENAME} -url {SITEURL}
3. Install the feature
stsadm -o installfeature -filename {FeatureFolder}\feature.xml
4. Activate the feature
stsadm -o activatefeature -id {FEATUREID} -url {SITEURL} -force

Nice
If had the batch one week earlier, that saved me much time.
But have you tried updating the assembly (the logic inside the execute method)? – Even if I uninstall and reinstall it after building the old assembly is executed until I change version number before reinstalling. Am I the only one with this problem and if not so is there an (automated) solution?
Kind regards
PS: I first had the tutorial from Andrew Connell and his installing instructions are a bit less for a SP noob like me – maybe you can tell him about your extension
Comment on March 10, 2008 @ 12:12:58
Hi Snap,
timer jobs are executed by a process called “Windows SharePoint Services Timer”.
Restart this service and your new assembly will be loaded.
You also have to attach to this service process to debug your job (OWSTIMER.EXE).
I hope this helps.
Comment on March 11, 2008 @ 13:00:59
Thanks for putting it all together. is this a c# template? i will appreciate if you can put up one for vb.net. i have tried it in vb.net but activating the feature keep throwing error. i have posted it on news group and no success on it. i could not find any one who has developed in vb.net yet. my client wants it in only vb.net so this is my contraint otherwise i have tested c# and it works fine.
Comment on March 19, 2008 @ 16:19:08
Thanks ,
but I have some question,If I want to carries out one time every day.I donot know how to do?
Could you give me any help?
Comment on April 2, 2008 @ 04:04:32
Hi Herry,
take a look into the file UpdateProfileDataJobInstaller.cs. There’s a function FeatureActivated. Here you can define the intervall your timer job is scheduled by. For example if you want your service to be run at 3 o’clock each mourning use the following lines to initialize the schedule:
SPDailySchedule ds = new SPDailySchedule();
ds.BeginHour = 3;
Comment on April 2, 2008 @ 09:13:24
Hi,
thanks, it was really usefull to me.
Comment on June 20, 2008 @ 09:06:47
hi
i have created the ‘Custom SharePoint Job’
in Visual studio
but when i try to build it it is giving
Error as
Error 1 Invalid argument. Culture ID 2155 (0x086B) is not a supported culture.
Parameter name: culture MISSPFileDownLoadJob
Comment on July 15, 2008 @ 14:27:40
[...] the most useful link i found was by Tobias Hertkorn. On this page you can download a fully functioning Visual Studio Templatet to create a SharePoint timer job and [...]
Pingback on August 1, 2008 @ 13:13:40
[...] deployment of a timer job is quite easy. You create easy if you have a visual studio template doing the dirty work for you. But redeploying it means that you have to restart the [...]
Pingback on August 18, 2008 @ 21:24:57
Hi Alex,
I am trying to use your template but I am running into a problem during the deploysolution command in your setup. I keep getting the error:
“This solution contains no resources scoped for a Web application and cannot be deployed to a particular Web application.”
I have tried modifying the feature.xml to set scope to Web, and WebApplication but neither work. The only way I can get it to deploy it seems is to do a global deploy from the Admin UI. What am I doing wrong?
Comment on August 19, 2008 @ 19:30:41
Hi Keith,
have you tried these steps:
1. Add the solution
stsadm -o addsolution -filename {WSPFILENAME}
2. Deploy the solution
stsadm -o deploysolution -name {WSPFILENAME} -url {SITEURL}
3. Install the feature
stsadm -o installfeature -filename {FeatureFolder}\feature.xml
4. Activate the feature
stsadm -o activatefeature -id {FEATUREID} -url {SITEURL} -force
Best regards,
Alex
Comment on August 20, 2008 @ 09:57:38
Yes. Step 2 fails everytime if I add the -url parameter, with this message “This solution contains no resources scoped for a Web application and cannot be deployed to a particular Web application.”
Comment on August 20, 2008 @ 16:01:31
Okay, give it another try with this line:
stsadm -o deploysolution -name {WSPFILENAME} -immediate -AllowGacDeployment
Best regards,
Alex
Comment on August 20, 2008 @ 16:24:25
HI, After Jobs deployment in Farm, I am getting event log error “Invalid URI : URI error” , among 3 servers only one is responding with JOB status success rest of all status showing failed. what could be the cause to raise this error.
We are using single dll for all 4 jobs, using WSP package. Please could you help me.
Thanks,
Nag.
Comment on September 3, 2008 @ 21:47:43
Hi,
My timer job has globally deployed.
I can activate/deactivate from farm.
Just to make sure it works, my timer job runs every minute and it has to write a simple line in event log.
I can view my timer job under definition in central admin.
However my timer job status always shows as failed.
Please help!
Comment on October 1, 2008 @ 16:49:42
Hi Nancy,
maybe your Timer Job throws an exeption.
Use the Debugger: Set a breakpoint in the Execute method and attach the debugger to the owstimer.exe.
Ensure that the SharePoint Timer Service was restarted after you deployed the timer job.
I hope this hels,
Alex
Comment on October 1, 2008 @ 19:33:23
Thank you for the speedy respond!
Here are the steps I did:
1. add the below code to my Execute():
#if (DEBUG)
System.Diagnostics.Trace.Assert(false);
#endif
2. Use the Debug: Attach to proccess
3. deploy
4. restart SharePoint Timer Service
Now here is a funny part:
Since my timer job is set to run every minutes, I get the message box every minute(based on step 1). Yet not only my timer job status still shows as failed but I still don’t get any line writen in my event log!!!!
Any solution?
Comment on October 1, 2008 @ 20:18:40
Nancy, you find any solution for this problem?
Comment on October 14, 2008 @ 11:00:52
Hi Alexander,
Great Post. I have gone through Andrew Connell’s post, but was freaky with the deployment part. I had tried everything from creating custom STSADM command to creating .wsp file. But could not get through a single one. But your template did it all.
I had few questions though. I looked into setup.bat file and by default it takes up local host, for having my own url, can I replace the DefaultWebUrl and DefaultSiteUrl. What is the role of TargetWebUrl and TargetSiteUrl.
Also if I want to uninstall the job how do I do it, can you please tell me?
Comment on October 22, 2008 @ 16:02:02
NOice, Nope I haven’t.
Comment on October 24, 2008 @ 21:16:13
Priyanka, here is what I did and it worked for me. Hopefully it works for you too:
1. You need to uninstall your feature first. you could do this in command prompt:
STSADM -o uninstallfeature -name featurename -force
2. retract solution:
STSADM -o retractsolution -name filename.wsp -immediate
3.
STSADM -o execadmsvcjobs
STSADM -o deletesolution -name filename.wsp -override
4.reset your iis
Comment on October 24, 2008 @ 21:20:58
I need help “how to” for the step #2. Create a new project using the “Custom SharePoint Job” template
Comment on November 3, 2008 @ 06:12:28
Hi! In the setup.bat, what will I put in the SPLocation if my development machine does not have the Sharepoint installed. I am just using virtual connection or remote server. Thanks!
Comment on November 10, 2008 @ 04:56:10
This is great code but I’ve run into a problem. The job worked great, then suddenly on running the bat file later it started giving this:
Failed to create feature receiver object from assembly “ReportDataRefresh, Version=1.0.0.0, Culter=neutral, PublicKeyToken=xxxetc”, type “TMSJobs.ReportDataRefreshInstaller” for feature [GUID]: System.ArgumentNullException: Value cannot be null.
Parameter name: type
at System.Activator.CreateInstance(Type tytpe, Boolean nonPublic)
at System.Activator.CreateInstance (Type, type)
at Microsoft.SharePoint.Adminstration.SPFeatureDefinition.get_ReceiverObject
I have no idea what is going on. Please help me!
Comment on November 10, 2008 @ 18:22:52
I forgot to mention this is after successfully adding the solution via the bat file, so this error shows up when the bat file tries to deploy the solution.
Thanks!
Comment on November 10, 2008 @ 18:24:27
I’m trying to do a simple thing: just to create a Timer Job that is displayed in Central Administration->operations->Job Definitions.Well, it’s created, it’s displayed in a list when I run
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
Console.WriteLine(job.Name);
But it’s not diplayed in the list on a Central administration site. Tell me please, what’s the problem?
Thanks!
Comment on November 23, 2008 @ 13:56:39
Hi everyone! I’ve created a custom timer job that is scheduled to run daily. It works well without a solution file. Except that I need to activate and deactivate it just to execute. It means that it doesn’t execute on its own daily. Is it because of my missing .wsp file? Also, I can’t see my timer job under the Central Administration. I just want to understand the essence of a solution file while I fix this. Thanks!
Comment on December 3, 2008 @ 11:57:48
Nicely documented, I was able to follow all steps and successfully deployed my solution. Thanks for every effort to explain and build the Timer Job template. Also, never mind about #22 (my earlier post).
Comment on December 3, 2008 @ 15:54:15
[...] Tools Visual Studio 2005 Timer Job Template The Timer Job Template may also be available online here. Timer Job Setup Drop the Timer Job Template (customsharepointjob.zip) into the C:Documents [...]
Pingback on December 11, 2008 @ 18:11:11
Do timer jobs always need a solution file (.wsp) to run? Thanks!
Comment on December 19, 2008 @ 10:23:22
[...] Project template for a custom timer job I have founded a template in http://saftsack.fs.uni-bayreuth.de/~…-jobs/142.html Thanks "Burlo" wrote: > sorry that second link should be [...]
Pingback on January 18, 2009 @ 19:40:27
Hi, I’ve installed the timer job with the setupb.bat given in the template. But I find this strange behaviour. After three/four runs of the timer job (scheduled each 2 minutes and simply writing DateTime.Now on a SPList), TimerJobs is executed twice, as I see two lines logged on the list each two minutes
Can anyone help?
Comment on February 6, 2009 @ 11:51:06
Hi Alex,
This is a great post. i have one question. when i run the successfully build bat file, it is activated on 80 port webapplication.what can i do, if i need it for another webapplication?
thanks
Naresh.
Comment on February 9, 2009 @ 15:37:32
Hi Alex,
How to uninstall this job. i have tried with below steps.
follwed steps for uninstall is:
1. You need to uninstall your feature first. you could do this in command prompt:
STSADM -o uninstallfeature -name featurename -force
2. retract solution:
STSADM -o retractsolution -name filename.wsp -immediate
3.
STSADM -o execadmsvcjobs
STSADM -o deletesolution -name filename.wsp -override
4.reset your iis
again it showing that the feature is already activated. what is the process to uninstall properly.
Comment on February 10, 2009 @ 16:35:41
@naresh: If you want to install and activate the timerjob feature in anoter site collection, set the parameter TargetWebUrl in the setup.bat to your site collection.
To uninstall the timer job simply call setup.bat /uninstall. The :LRetract section contains the commands to deactivate the feature (stsadm -o deactivatefeature).
Comment on February 10, 2009 @ 16:47:49
Thanx alex,
Comment on February 11, 2009 @ 14:01:43
I see the note: “Be careful to not use a dot in the project name.” I’m creating a timer job and the project names is [Solution].Jobs as is the namespace and other things. is this dot going to mess me up? I currently can’t get the timer job to install and I’m wondering if that is my issue.
Comment on February 18, 2009 @ 00:19:47
Hi Alex,
I have created Timer job on 80 port webapplication. suppose i will activate this job on another webapplication. can i get the weapplications list(urls) at runtime(in execute method using sharepoint object model), on which webapplications this job is running ?
Thanks
Naresh
Comment on February 25, 2009 @ 15:51:50
Hi,
I get the following error after I run the setup
Invalid URL: http://localhost. You may also need to update any alternate access mappings referring to http://priyankawss. Help on this error: http://go.microsoft.com/fwlink/?LinkId=114854
I have a site at http://localhost.But the application isnot abletorecognize it.
I have set the columns as
set DefaultWebUrl=http://localhost
set DefaultSiteUrl=http://localhost
set TargetWebUrl=
set TargetSiteUrl=
Am I missing something?
Should I carry out any other configuration?
Thanks,
Priyanka
Comment on March 16, 2009 @ 03:56:41
How Can I access the web.config ?
Comment on June 15, 2009 @ 15:01:16
Hi Alexander,
Thanks for the nice post. I have a problem with my timer job service as Snap252 one. Windows Sharepoint Service timer is unable to take the latest assembly from GAC. I tried restarting IIS, Windows sharepoint service timer, etc but it didn’t help until i change the version number.
Does anyone has solution for this?
Thanks.
Comment on July 20, 2009 @ 18:20:57
Hi Schaf,
the Timer Jobs are not run by the IIS. You have to restart the Windows Service “SharePoint Services Timer Service” known as OWSTIMER.EXE. Just restart it!
Regards,
Alex
Comment on July 20, 2009 @ 19:25:03
Hi Alex,
Thanks for your fast response. I did restart OWSTIMER.exe several times but in Timer Job Definitions i still see old declarations. I changed my minute schedule to hourly schedule, then it always showing minutes schedule.
Is there anyway to reflect the latest in timer job definition in Central Admin?
Thanks
Comment on July 21, 2009 @ 09:45:02
Hi Alex,
I have created a timer job.The is a Daily scheduled job.
The issue is that at the scheduled time the “EXECUTE” methot is running twice.
What could be the issue.
“Please let me know if you need any more information regarding the CODE”
thanks
Comment on August 5, 2009 @ 15:03:27
is it possible to write timer job in sharepoint portal server 2003 as well?
Comment on August 19, 2009 @ 05:38:37
As far as I know, Timer Jobs are new to SharePoint 2007.
Comment on August 19, 2009 @ 08:58:34
Hi, ive used your template in updating a sharepoint list. i just modified the EXECUTE method to update my list. Compiled, deployed run, The timer job works well(run), but still not updating my list. my code is 100% working (tested it to run in console and produced the right output in my list) but it wont execute in your template(just modified EXECUTE method, namespace)… Thanks…
Comment on August 21, 2009 @ 09:48:57
i mean my code wont execute(produce the expected output) as a Timer job using your template. thanks.
Comment on August 21, 2009 @ 09:50:50
i need help… anyone please???
Comment on September 1, 2009 @ 10:16:57
Hi bk,
have you already debugged your job? Attach your Visual Studio debugger to the process OWSTIMER.EXE and have a look what’s going on.
Maybe your Job has not enough permissions. The job is executed with the credentials of the Timer Job Service.
Regards,
Alexander
Comment on September 1, 2009 @ 10:59:14
is there any other way??? im not familiar with OWSTIMER.exe… im a total newbie in this MOSS programming…
Comment on September 2, 2009 @ 06:05:54
when does the overriden Execute method gets to be executed??? i dont see any call to this method???
Comment on September 2, 2009 @ 08:46:01
Maybe you should read this article to get some background about timer job development:
http://msdn.microsoft.com/en-us/library/cc406686.aspx
The execute method is executed as defined in the installer class. The owstimer.exe ist your host application that loads your timer job. Take a look into your central administration if your timer job definition is listed and if you have a timer job status. When you redeploy your timer job ensure to resart the timer job windows service. If you don’t do this it will still execute your old version.
Comment on September 2, 2009 @ 10:24:15
Hi Alex,
why not add
echo Restart OSTimer.exe
net stop SPTimerV3
net start SPTimerV3
to setup.bat/LDeploy?
Regards,
Henk
Comment on February 9, 2010 @ 18:51:37
Howabout VS 2008?
Comment on February 25, 2010 @ 04:05:15
Hey,
Need some help. I want to create a Timer Job in sharepoint 2007 which will just hit a url.
Ex: http://abc.com?do=process
I want the above url to be hit every time the job executes. How to do?
Thanks in advance.
Sumeet
Comment on June 21, 2010 @ 11:12:50
This is the most awesome template ever see, it works like charm.
Big Thank You Maestro !!!
A
Comment on April 29, 2011 @ 10:53:26
Thanks
It helped me a lot…
Comment on May 23, 2011 @ 17:06:22
This post is bull shit!!!. It won’t work at all. The person who posted this is a crap.
Comment on June 8, 2011 @ 15:43:56
Sharepoint timer job should not be implemented like this. It’s a failure. This post has to be removed.
Comment on June 8, 2011 @ 15:44:56
Those are quite some strong emotions you express here, shankar.
I’m sorry… no … I pity you for not being able to give constructive feedback. I respect your opinion and therefore won’t delete your comments. Even though I am itching to do so. Learn to deal with your emotions though, before you come back to my house again, aight?Comment on June 9, 2011 @ 10:05:52
how about visual studio 2008? is there any template for the timer jobs in vs2008?
Comment on July 3, 2012 @ 09:27:39