Below is the skeleton of that class.
namespace TimerNameSpace { #region Using using System; using System.Collections.Generic; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; using Microsoft.SharePoint.Utilities; #endregion Using /// <summary> /// Timer service /// </summary> public class MyTimerService : SPJobDefinition { #region Constructor /// <summary> /// Initializes a new instance of the TimerService class /// </summary> public MyTimerService() : base() { } /// <summary> /// Initializes a new instance of the MyTimerService class /// </summary> /// <param name="jobName">Job Title for the timer service</param> /// <param name="webApp">Web appication for which this service will run</param> public MyTimerService(string jobName, SPWebApplication webApp) : base(jobName, webApp, null, SPJobLockType.ContentDatabase) { this.Title = "My Timer"; } #endregion Constructor #region Execute /// <summary> /// Overriden Execute method /// </summary> /// <param name="targetInstanceId">guid of the target object</param> public override void Execute(Guid targetInstanceId) { try { /// Here the timer logic goes } catch (Exception ex) { //Log exception } } #endregion Execute } }One this class is ready. Next thing we need to look into is creating a Feature. This feature will internally schedule the timer as a Job and runs it. Below is the snapshot of this class.
namespace TimerNameSpace { #region Namespace Inclusions using System; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; #endregion Namespace Inclusions /// <summary> /// Class is feature receiver for feature to install approval timer service /// </summary> public class MyTimerFeature : SPFeatureReceiver { /// <summary> /// Notifications job name /// </summary> public string MyTimerFeatureName = "MyTimerServiceFeature"; /// <summary> /// Occurs after a Feature is installed. /// </summary> /// <param name="properties">An object that represents the properties of the event.</param> public override void FeatureInstalled(SPFeatureReceiverProperties properties) { } /// <summary> /// Occurs when a Feature is uninstalled. /// </summary> /// <param name="properties">An object that represents the properties of the event.</param> public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { } /// <summary> /// Occurs after a Feature is activated. /// </summary> /// <param name="properties">An object that represents the properties of the event.</param> public override void FeatureActivated(SPFeatureReceiverProperties properties) { // register the the current web SPWeb web = (SPWeb)properties.Feature.Parent; SPSite site = web.Site; // make sure the job isn't already registered foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) { if (job.Name == this.MyTimerFeatureName) { job.Delete(); break; } } MyTimerService myJob = new MyTimerService(this.MyTimerFeatureName, site.WebApplication); SPMinuteSchedule schedule = new SPMinuteSchedule(); schedule.BeginSecond = 0; schedule.EndSecond = 5; schedule.Interval = 2; myJob.Schedule = schedule; myJob.Update(); } /// <summary> /// Occurs when a Feature is deactivated. /// </summary> /// <param name="properties">An object that represents the properties of the event.</param> public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { // current web SPWeb web = (SPWeb)properties.Feature.Parent; SPSite site = web.Site; // delete the job foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) { if (job.Name == this.MyTimerFeatureName) { job.Delete(); break; } } } } }Similar to SPMinuteSchedule, there is Hourl Schedule also. Use the one as per your requirement.
Once this much is done, we need to create Feature.xml file for the feature we have built and paste it under the Features Folder (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\FEATURES). Create a folder named MyTimerFeature and under this folder create Feature.xml with the following content.
<?xml version="1.0" encoding="utf-8" ?> <Feature xmlns="http://schemas.microsoft.com/sharepoint/" Creator="Amal Hashim" Id="{76D2F200-5CA0-4882-B64F-9FC6208C1234}" Title="MyTimerFeature" Description="My Timer." Scope="Web" Hidden="FALSE" Version="1.0.0.0" ReceiverAssembly="MyTimer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=12344ab009fa4028" ReceiverClass="TimerNameSpace.MyTimerFeature"> </Feature>You can activate the feature using the Stsadm command as shown below.
stsadm -o installfeature -name MyTimerFeature
For checking whether the timer has activated successfully or not, you can go to the central admin page
Central Administration –> Operations –> Timer Job Status