Tuesday, May 14, 2013

Creating Custom Timer Job in SharePoint 2010

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
  1. Start Visual Studio 2010, and then create a new Empty SharePoint Project, as shown in the following illustration. Name the project SimpleTimerJob.
    Ff798313.e7a6f9df-8ae8-47b1-8557-70819493a521(en-us,PandP.10).png
  2. In the SharePoint Customization Wizard, specify a valid local site for debugging, select Deploy as a farm solution, and then click Finish.
    Ff798313.cea70e72-c1e9-4196-b729-bb9e97c17126(en-us,PandP.10).png

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
  1. Add a new class named SimpleJobDefinition to the project.
  2. Add the following using statement to the class.
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    
    
  3. Add the public access modifier to the class.
  4. Modify the class to inherit from the SPJobDefinition class. Your class should resemble the following code.
    public class SimpleJobDefinition : SPJobDefinition { }
    
    
  5. Within the SimpleJobDefinition class, add a public constant string named JobName.
    public const string JobName = "SimpleJobDefinition"; 
    Note: You must provide a job name when you add or remove a job definition. By defining the job name as a constant in the job definition class, you ensure that it is always available and remains unchanged.
    
    
  6. 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.

    public SimpleJobDefinition() : base() { }
    
    
  7. 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.

    public SimpleJobDefinition(SPWebApplication webApp) :
      base(JobName, webApp, null, SPJobLockType.Job) { }
    Note: The SPJobDefinition base constructor provides alternative overloads that you can use if you want to scope your timer job to an SPServiceinstance instead of a Web application. For more information, see SPJobDefinition Class on MSDN.
  8. Within the constructor, give your timer job a title. This specifies how your timer job appears in the SharePoint Central Administration Web site.
    Title = "Simple Job Definition";
    
    
  9. 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.
    public override void Execute(Guid targetInstanceId)
    {
    }
    
    
  10. 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.
    public override void Execute(Guid targetInstanceId)
    {
      // Execute the timer job logic.
      SPWebApplication webApp = this.Parent as SPWebApplication;
      SPList taskList = webApp.Sites[0].RootWeb.Lists["Tasks"];
      SPListItem newTask = taskList.Items.Add();
      newTask["Title"] = DateTime.Now.ToString();
      newTask.Update();
    }
    
    

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
  1. In Solution Explorer, right-click the Features node, and then click Add Feature.
  2. In Solution Explorer, right-click the new feature node, and then click Rename. This example uses the name SimpleJobFeature for the feature.
  3. Double-click the SimpleJobFeature node to open the feature designer window, and then set the scope of the feature to WebApplication.
  4. Add an event receiver to the SimpleJobFeature. To do this, right-click SimpleJobFeature in Solution Explorer, and then click Add Event Receiver.
  5. In the SimpleJobFeatureEventReceiver class, add the following using statement.

    using Microsoft.SharePoint.Administration;
    
    
  6. 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.
    private void DeleteJob(SPJobDefinitionCollection jobs)
    {
      foreach(SPJobDefinition job in jobs)
      {
        if(job.Name.Equals(SimpleJobDefinition.JobName, 
        StringComparison.OrdinalIgnoreCase))
        {
          job.Delete();
        }
      }
    }
    
    
  7. Uncomment the FeatureActivated method.
  8. In the FeatureActivated method, add the following code to register the job definition, and set the job schedule to run once every minute.
    SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
    DeleteJob(webApp.JobDefinitions);
     
    SimpleJobDefinition simpleJob = new SimpleJobDefinition(webApp);
     
    SPMinuteSchedule schedule = new SPMinuteSchedule();
    schedule.BeginSecond = 0;
    schedule.EndSecond = 59;
    schedule.Interval = 1;
     
    simpleJob.Schedule = schedule;
    simpleJob.Update();
    
    
  9. Uncomment the FeatureDeactivating method.
  10. In the FeatureDeactivating method, add the following code to remove the job definition from the Web application.
    SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
    DeleteJob(webApp.JobDefinitions);
    
    
  11. 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.
    Ff798313.ed00bd65-e0be-4521-a9d1-3dbddb6ad457(en-us,PandP.10).png
    Reference:

No comments:

Post a Comment

SharePoint Deployment Error : The specified path, file name, or both are too long.

Hi, Sometimes we get surprised with below error while deploying wsp file... Solution : To overcome with this issue, we just ne...