Tuesday, April 19, 2011

Sample SharePoint timer Job code

Feature Code - TimerJob.cs

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

namespace CancellationTimer
{
class CancellationTimerJob : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
String TaskName = "Task Creator";

// make sure the job isn't already registered
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == TaskName)
job.Delete();
}

// install the job
TaskTimerJobImplementation oCreateTaskTimerJob = new TaskTimerJobImplementation(TaskName, site.WebApplication);



SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 1;

oCreateTaskTimerJob.Schedule = schedule;

oCreateTaskTimerJob.Update();
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
String TaskName = "Task Creator";

// make sure the job isn't already registered
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == TaskName)
job.Delete();
}
}

public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
throw new Exception("The method or operation is not implemented.");
}

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
throw new Exception("The method or operation is not implemented.");
}
}
}


TimerClass.cs----------------------------------------------



using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
namespace CancellationTimer
{
public class TaskTimerJobImplementation :SPJobDefinition
{
public TaskTimerJobImplementation()
: base()
{

}

public TaskTimerJobImplementation(string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base(jobName, service, server, targetType)
{

}

public TaskTimerJobImplementation(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = "Task Creator";
}

public override void Execute(Guid targetInstanceId)
{
base.Execute(targetInstanceId);

// get a reference to the current site collection's content database
SPWebApplication oWebApp = this.Parent as SPWebApplication;
SPContentDatabase oConDB = oWebApp.ContentDatabases[targetInstanceId];

// get a reference to the "Tasks" list in the RootWeb of the first site collection in the content database
SPList taskList = oConDB.Sites[0].RootWeb.Lists["TestingToDelete"];

// create a new task
SPListItem newTask = taskList.Items.Add();
newTask["Title"] = "Auto Task : " + DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
newTask.Update();
}
}
}


Thanks to

http://rajasinghtechshare.blogspot.com/2010/04/how-to-create-custom-timer-jobs-in.html
http://www.mssharepointtips.com/tip.asp?id=1021
http://community.zevenseas.com/Blogs/Robin/Lists/Posts/Post.aspx?ID=87