Sunday, August 5, 2012

How to create custom timer job in SharePoint 2010

Timer jobs are executable tasks that run on one or more servers at a scheduled time.They can be configured to run exactly one time, or on a recurring schedule. They are similar to task scheduler in windows and  Microsoft SQL Server Agent jobs, which maintain a SQL Server installation by backing up databases, defragmenting database files, and updating database statistics. SharePoint uses timer jobs to maintain long-running workflows, to clean up old sites and logs, and to monitor the farm for problems. The Windows SharePoint Services Timer service runs the timer jobs in your farm. The service must be enabled and running on each server in your farm. The service enables the various SharePoint timer jobs to configure and maintain the servers in the farm. If you stop the Windows SharePoint Services Timer service on a server, you also stop all SharePoint timer jobs running on that server.


Here i want to create a sample timer job i.e like for every 5 minutes i wanna to add list item to list. Complete code available at the end of the post.

1. Create one sample list i.e ListTimerJob.


2. Now Open visual studio 2010 or 2012 RC, click on new project.

3. In new project window, select 2010 under SharePoint and select SharePoint 2010 project. Name the project as "SP_CustomTimerJob".

4. Specify SharePoint portal address for debugging location and select Deploy as a farm solution.

5. Right click on project and add class 'ListTimerJob.cs'.

6. now add Microsoft.SharePoint & Microsoft.SharePoint.Administrator namespace to 'ListTimerJob.cs'.


and inherit 'SPJobDefinition'.Implement 3 different types of constructors and override Excute method.




7. Now add feature to the project.

here u can change feature name and set scope of the feature.

8. Now right click on Feature and add Event Receiver.

9. Add Microsoft.SharePoint.Administrator namespace to the project.

Uncomment FeatureActivated and FeatureDeactivating methods in EventReceiver of the feature.

Now add your action code in Feature activated like specifying job definition and schedule time.


10. Now deploy the project.


11. Now you can open ListTimerJob list , for every five minutes one list item adding with system data time as Title.


we can see our newly added timer job definition in central administration.

a. open central administration , click on "monitoring".

b. Lick on Review job definition under TimerJobs.

c. Now you can see our ListTimerJob along with associate web application and schedule interval.


Finally list will automatically adds one new item for every 5 minutes. this is sample example like this we can write any sort of code which will automatically runs.

i will update new type of examples on timer job shortly.. Thank you..


Complete code available here

No comments:

Post a Comment