Monday, September 19, 2011

    CREATING A TIMER JOB CLASS
     
 
SharePoint Timer Jobs can lets you create & execute required operations on a regular basis. After deployment, the timer job definition executes your custom code.

There are several ways to deploy a Custom Timer Job. The most commonly used way to deploy a timer job is by feature. Once the feature is installed, it can be activated and deactivated based on the requirement.


This is the first article in the 4 part series about Custom Timer Jobs.
  1.            Creating Custom Timer Job-Part1(Custom Timer Job Class) 
  2.        Creating Custom TimerJob-Part2(Feature Event Reciever Class)
  3.       Creating Custom TimerJob-Part3(Deploying the Custom Timer Job)
  4.       Creating Custom Timer Job-Part4(Debugging and Error Handling)



Creating a Timer Job Class

Steps:
  •         Create a new project. Add to it class say ‘SampleTimer’ for writing your timer code.
  •         The next step is to add the ‘Microsoft.SharePoint.dll’ reference.
  •         Inherit the ‘SPJobDefinition’ Interface.
  •         Define few constructors (both parameterized and non-parameterized) so as to provide job details.
  •         Impliment the ‘Execute()’ method where we write the required logic during the timer execution.
  •         The above method provides the ID for the content database of the application. From this ID, we can obtain the web application, and then, the required site collection and so on.



//Code for Timer Job Class

using System;
using System.Collections.Generic;

using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace TimerJobExmaple
{
    class SampleTimer: SPJobDefinition
    {


#region constructors

        publicSampleTimer()
            : base()
        {

        }
        public SampleTimer(string jobName, SPService service, SPServer server,    SPJobLockType     targetType)

            :  base(jobName, service, server, targetType)
        {

        }
       public SampleTimer(string jobName, SPWebApplication webApplication)

            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {

            this.Title = "List Timer Job";

        }


#endregion




#region ExcuteMethod

        public override void Execute(Guid contentDbId)
        {

     
            try
            {
               //Write your logic here
               
                SPWebApplication mywebapp = this.Parent as SPWebApplication;
                SPSite mysitecol = mywebapp.Sites["<Site URL"];
            
                SPList mylist = mysitecol.RootWeb.Lists["Tasks"];
              

                SPListItem mytiem = mylist.Items.Add();
                mytiem["Title"] = DateTime.Now.ToString();
              
                mytiem.Update();
                mylist.Update();
            }
            catch (Exception e)
            {

            }
           
        }


#endregion



    }
}


That’s it. Our first task is completed.
Do look out for the remaining part of this tutorial.