This Article target's on development and deployment of timer job in SharePoint.
Lets begin creating a blank project and calling references of SPJobDefinition for assigned timer that will execute the functionality as needed by the timer job to execute. Along with this we are also creating a feature which will specify the trigger schedule.
Here I’m specifying the code snippet for the same.
Create a Class library project include all the SharePoint related references needed.
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
DEFINE JOB
Inherit the SPJobDefinition class which is a member for Sharepoint Administration
class ClassName:SPJobDefinition
{
internal const string TASK_NAME = "MYTimer" ;
public ClassName() : base(){
}
public ClassName (string jobName, SPService service, SPServer server, SPJobLockType targetType): base(jobName, service, server, targetType){
}
public ClassName (string jobName, SPWebApplication webApplication)
: base((jobName, webApplication, null, SPJobLockType.Job) {
this.Title = "MYTimer" ;
}
//Override the Execute function
public override void Execute(Guid targetInstanceId) {
//Calling our function which needs to be executed
MYfunction();
//base.Execute(targetInstanceId);
}
public void MYfunction() {
try
{
SPSite osite = new SPSite( "SiteURL" );
//SPSite osite = SPContext.Current.Site;
SPWeb oweb = osite.OpenWeb();
oweb.AllowUnsafeUpdates = true;
if (oweb.Lists[ "Tasks" ] != null)
{
SPList olist = oweb.Lists[ "Tasks" ];
SPListItem newTask = olist.Items.Add();
newTask[ "Title" ] = DateTime.Now;
newTask.Update();
}
}
catch (Exception ee) { //Add you Exceptions catch }
}
}
Add a new class item for Feature Creation
//Feature creation
class MyclassInstaller:SPFeatureReceiver {
internal const string TASK_NAME = "MYTimer" ;
public override void FeatureInstalled(SPFeatureReceiverProperties properties) {
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties) {
}
public override void FeatureActivated(SPFeatureReceiverProperties properties) {
// register the the current web
SPSite site = properties.Feature.Parent as SPSite;
// make sure the job isn't already registered
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == TASK_NAME)
job.Delete();
}
TaskLoggerJob taskLoggerJob = new TaskLoggerJob(TASK_NAME, site.WebApplication);
// For Minute Trigger
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
//Intervals define the Minute Time Gap between triggers ex-2= min gap for timer trigger
schedule.Interval = 2;
taskLoggerJob.Schedule = schedule;
taskLoggerJob.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
// delete the job
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == TASK_NAME)
job.Delete();
}
}
}
DEPLOYMENT
The best way to deploy the time project is by creating a WSP either can use WSP builder or NANT for creating a WSP.
But Before we deploy this solution we need to add its FEATURE.XML in 12 hives feature folder with naming Feature conventions as here i have termed "MyTimer". You can manually add this file in this folder structure with following tags in it :
Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="DA1B534E-08D9-41ef-B2C4-B656E9389D80"
Title="MYTimer" Description="Installs the task MY timer job feature to the current site collection."
Scope="Site"
Hidden="TRUE"
Version="1.0.0.0"
ReceiverAssembly="MYSite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3aeda64f0b993534"
ReceiverClass="MYSite.MyclassInstaller
>
Once a WSP is created you follow the same procedures as recommended i.e
1. Add Solution
2. Deploy Solutions
3. Install Feature
4. Activate Feature.
STSADM Commands for WSP Deployment
pushd %programfiles%\common files\microsoft shared\web server extensions\12\bin
@Echo ------Adding Sol------
stsadm -o addsolution -filename file://wsppath%20/MyTimer.wsp
stsadm -o execadmsvcjobs
@Echo ------deploy Sol----
stsadm -o deploysolution -name MyTimer.wsp -allowGacDeployment -local -force
stsadm -o execadmsvcjobs
iisreset
@echo -----Feature added-----
stsadm -o installfeature -filename MyTimer\feature.xml -force
stsadm -o activatefeature -filename MyTimer\feature.xml -url siteURL -force
stsadm -o execadmsvcjobs
iisreset
There goes you can check timer execution in Timer job status & Timer job definitions in central administration which details the execution time and its status for success.
Just a Quick information of all the inbuilt Timer in sharepoint Sorted by Neil Click here
Here are the snipplets for Weekly, Monthly & Annual.
Hourly(Triggers @ Every Hour 01.Min)
SPHourlySchedule JobSchedule = new
SPHourlySchedule();
JobSchedule.BeginMinute = 1;
JobSchedule.EndMinute = 2;
Weekly (Triggers @ Thursday 04:01:00PM)
SPWeeklySchedule JobSchedule = new SPWeeklySchedule();
JobSchedule.BeginDayOfWeek = DayOfWeek.Thursday;
JobSchedule.EndDayOfWeek = DayOfWeek.Thursday;
JobSchedule.BeginHour = 16;
JobSchedule.EndHour = 16;
JobSchedule.BeginMinute = 01;
JobSchedule.EndMinute = 05;
JobSchedule.BeginSecond = 00;
JobSchedule.EndSecond = 00;
taskLoggerJob.Schedule = JobSchedule;
SPMonthlySchedule JobSchedule = new SPMonthlySchedule();
JobSchedule.BeginDay = 1;
JobSchedule.EndDay = 1;
JobSchedule.BeginHour = 10;
JobSchedule.EndHour = 10;
JobSchedule.BeginMinute = 15;
JobSchedule.EndMinute = 25;
JobSchedule.BeginSecond = 00;
JobSchedule.EndSecond = 00;
Annually (Triggers @ April 21, at 10:15 AM)
SPYearlySchedule JobSchedule = new SPYearlySchedule();
JobSchedule.BeginMonth = 4;
JobSchedule.EndMonth = 4;
JobSchedule.BeginDay = 21;
JobSchedule.EndDay = 21;
JobSchedule.BeginHour = 10;
JobSchedule.EndHour = 10;
JobSchedule.BeginMinute = 15;
JobSchedule.EndMinute = 25;
JobSchedule.BeginSecond = 00;
JobSchedule.EndSecond = 00;
Addin your comments for this article.
Special Thanks: Andrew Connell, Ganesh Bankar.
Does it work in SP2010?
ReplyDeleteDo you have a sample of this project? (Something I can download)
Thanks in advanced!
good one
ReplyDeleteScheduler for timeline view in .NET
ReplyDelete