Step 1: Create the SharePoint Project
This procedure creates a SharePoint project in Visual Studio 2010. You can use this project to build and deploy your timer job assembly.
To create a sandbox proxy project
- Start Visual Studio 2010, and then create a new Empty SharePoint Project, as shown in the following illustration. Name the project SimpleTimerJob.
- In the SharePoint Customization Wizard, specify a valid local site for debugging, select Deploy as a farm solution, and then click Finish.
Step 2: Create the Job Definition Class
This procedure describes how to create a job definition class. The job definition class encapsulates your timer job logic.
To create a job definition class
- Add a new class named SimpleJobDefinition to the project.
- Add the following using statement to the class.
- Add the public access modifier to the class.
- Modify the class to inherit from the SPJobDefinition class. Your class should resemble the following code.
- Within the SimpleJobDefinition class, add a public constant string named JobName.
- Add a default constructor to the class that inherits from the default constructor of the base class. This is required for the serialization and de-serialization of your timer job.
- Add a constructor that accepts an argument of type SPWebApplication, as shown in the following code. This enables the base SPJobDefinition class to instantiate your timer job within a specific Web application.
- Within the constructor, give your timer job a title. This specifies how your timer job appears in the SharePoint Central Administration Web site.
- Within the SimpleJobDefinition class, override the Execute method. The method should accept an argument of type Guid. In this case, the GUID represents the target Web application.
- Add your timer job logic to the Execute method. This example simply adds an item to the task list on the root site of the Web application. The job logic sets the title of the task to the current date and time.
Step 3: Create a Feature to Register the Job
This procedure describes how to use a feature receiver class to install the timer job in your SharePoint environment.
To register a Web application-scoped timer job
- In Solution Explorer, right-click the Features node, and then click Add Feature.
- In Solution Explorer, right-click the new feature node, and then click Rename. This example uses the name SimpleJobFeature for the feature.
- Double-click the SimpleJobFeature node to open the feature designer window, and then set the scope of the feature to WebApplication.
- Add an event receiver to the SimpleJobFeature. To do this, right-click SimpleJobFeature in Solution Explorer, and then click Add Event Receiver.
- In the SimpleJobFeatureEventReceiver class, add the following using statement.
- Add a method named DeleteJobs that accepts an argument of type SPJobDefinitionCollection. The method should iterate through the job definition collection and delete any instances of SimpleJobDefinition.
- Uncomment the FeatureActivated method.
- In the FeatureActivated method, add the following code to register the job definition, and set the job schedule to run once every minute.
- Uncomment the FeatureDeactivating method.
- In the FeatureDeactivating method, add the following code to remove the job definition from the Web application.
- Press F5 to debug your timer job. After a few minutes, browse to the task list on your root site and verify that the timer job has added a task once in every one minute period.Reference: