How to create Timer job in MOSS

No.of Views1457
Bookmarked0 times
Downloads 
Votes0
By  amalhashim   On  28 Apr 2010 10:04:28
Tag : SharePoint , Development and Programming
How to create Timer job in MOSS
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introducation

First we need to have a class derived from SPJobDefinition,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

That's all, i hope this is help to all.

 
Sign Up to vote for this article
 
About Author
 
amalhashim
Occupation-Software Engineer
Company-Aditi Technologies
Member Type-Senior
Location-Not Provided
Joined date-07 Jun 2009
Home Page-http://lamahashim.blogspot.com
Blog Page-http://lamahashim.blogspot.com
I have done my masters in Computer Applications and graduation in Computer Science. I have great passion in working with Microsoft tool and technologies. I am also a Microsoft Most Valuable Professional. Personally my objective is to design/develop applications which eases user experience and performs better in long run.
 
 
Other popularSectionarticles
Comments
By:KirtiDate Of Posted:4/29/2010 11:29:54 PM
Good
Good Keep it Up
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top