Monday, January 3, 2011

Create a Custom Timer Job in SharePoint 2010


I had to create a custom timer job in SharePoint 2010 recently, but could not find a blog which explained the complete procedure to do this. So I decided to write one. In this post I will give a step by step process to create a Custom timer job in SharePoint 2010 using Visual Studio 2010.

Timer jobs are created by extending the SPJobDefinition class and deployed as features. So we will create a class which extends the SPJobDefinition class. Then we will create a feature and we will handle the featureActivated() event of that feature. In that we will instantiate the class created. Let us see how to do all this.

Create an empty SharePoint project in VS 2010.


Give the Site Collection URL where you want this feature will be activated.
Create a class in the project.


 
Add these namespaces
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;


class MyTimerJob:SPJobDefinition
{
public MyTimerJob() : base() { }

public MyTimerJob(SPWebApplication webApp)
                : base("MyTimerJob", webApp, null, SPJobLockType.ContentDatabase)
         {
                this.Title = "MyCustomTimerJob"; // create the timer job with the name passed in Constructor and assign title
         }

         public override void Execute(Guid targetInstanceId)
         {
SPWebApplication app = this.Parent as SPWebApplication; 
SPList announcements = app.Sites[0].RootWeb.Lists["Announcements"]; // get reference of the lists
                   app.Sites[0].RootWeb.AllowUnsafeUpdates = true;

                   // you code to work with the lists
}
}

 
Right click on the Features and click on Add Feature.
Then right click on Features.Feature1 and click on Add Event Receiver


Handle the featureActivated() event and instantiate the timer job created. Assign a schedule for it.
Also handle the featureDeactivating() event and write the logic to delete the timer job when the feature is deactivated.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
          // create the timer job when the feature is activated
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication; // get the web application reference

foreach (SPJobDefinition job in webApp.JobDefinitions)
          if (job.Name == "MyTimerJob") job.Delete();

MyTimerJob myJob = new MyTimerJob(webApp); // create the new timer job
// here you can assign the schedule as per your requirement. I have created a daily schedule
SPDailySchedule schedule = new SPDailySchedule(); // create a daily schedule to run the timer job
schedule.BeginHour = 1;
schedule.EndHour = 2;

myJob.Schedule = schedule;
myJob.Update(); // assign the schedule to the timer job
}


public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

foreach (SPJobDefinition job in webApp.JobDefinitions)
if (job.Name == "MyTimerJob") job.Delete();
}


Change the scope of the feature to whatever applicable and deploy the solution.
Go to central administration and see if you can see the timer job that you created.
 

8 comments:

  1. I get following exception:
    The parameter is incorrect

    ReplyDelete
  2. Have you tried debugging your code. Please be more specific about where you get the error.

    ReplyDelete
  3. There are certainly a lot more details to take into consideration, but thanks for sharing this post.
    Sharepoint Consulting is evolving with a lightning speed day by day as companies are growing. As companies grow, it is difficult to manage the record of files, documents, and their location in the companies. SharePoint Development helps you to store the files and documents and share it on a central site.

    ReplyDelete
  4. what if i want to create a custom schedule to run the timer job. i mean i want to run a timer job on weekdays and not to run on saturday and sunday...

    ReplyDelete
  5. ITS HELPS TO ME SO MUCH
    THANKS BOSS......


    IF ANY PUBLISH INTIMATE Me

    ReplyDelete
  6. I actually enjoyed reading through this posting.Many thanks.
    Sharepoint 2010 Archiving

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. I'm enjoying it right now and well this is genius. I have also tried Clear Timer App and this one's also really got a bang. I'm really grateful with these certain products of technology.

    ReplyDelete