Wednesday, April 21, 2010

Sharepint 5 - Use SPJobDefinition to schedule a Job

Build a Console Application, reference Microsoft.Sharepoint.dll

Ex 1. If we want to get all the scheduled jobs in a site:

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

class Program
{
static void Main(string[] args)
{

SPSite mySite = new SPSite("http://mysite:port");

foreach (SPJobDefinition job in
mySite.WebApplication.JobDefinitions)
{
Console.WriteLine("JobName: {0} , Last Run time: {1}, Type: {2}",job .Title,job.LastRunTime.ToLongTimeString(),job.Schedule.GetType().ToString()));
}
}
}

Ex 2. If we want to delete a scheduled job.

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

class Program
{
static void Main(string[] args)
{

SPSite mySite = new SPSite("http://mysite:port");

foreach (SPJobDefinition job in
mySite.WebApplication.JobDefinitions)
{
if(job.Title=="My Job")
{job.Delete();}
}
}
}

Ex 3. Now let's talk about Create a Job, and Register and Schedule it.

Create a Console Application, reference Microsoft.Sharepoint.dll, Add a new item class, call this Class MyJob.

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

public class MyJob:SPJobDefinition
{
public MyJob() { }

//Below we can override the constructor
public MyJob(string myJobName, SPWebApplication myWebApplication) : base(myJobName, myWebApplication, null, SPJobLockType.Job) { }


//Notice the key point is to override the Execute Method and
//place our job code inside it.
public override void Execute(Guid ConetentDataBaseID)
{

SPSite mySite = new SPSite("http://mysite:port");
SPWeb myWeb = mySite.OpenWeb();
SPList myList = myWeb.Lists["{***}"];


SPListItem newitem = myList.Items.Add();
newitem["Title"] = "Insert An item Every Day"
newitem.Update();

}
}

This is just a sample job to insert to Lists["{***}"] an item every day, now we in Main entry to register this job and schedule the job using SPDailySchedule.

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

static void Main(string[] args)
{
SPServer myServer = SPServer.Local;
SPSite mySite = new SPSite("http://site:port");
SPWebApplication myApplication = mySite.WebApplication;
SPJobDefinitionCollection myJobsColl = myApplication.JobDefinitions;


MyJob myJob = new MyJob("myJob", myApplication);
myJob.Title = "This is A Test Job";

SPDailySchedule myDailySchedule = new SPDailySchedule();
myDailySchedule.BeginHour = 12;
myDailySchedule.BeginMinute = 59;
myDailySchedule.BeginSecond = 0;
myDailySchedule.EndHour = 12;
myDailySchedule.EndMinute = 59;
myDailySchedule.EndSecond = 0;

myJob.Schedule = myDailySchedule;

myJobsColl.Add(myJob);
myApplication.Update();

}

In this case we build a job and register and scheduled it, it runs 12:59:00 every day.

Notice that you need to specify both the Begin and End Times. Setting the BeginHour, BeginMinute, and BeginSecond without specifying the EndHour, EndMinute, and EndSecond values will prevent the timer job from running. No error is thrown, just no execution.

You can also use FromString method of SPSchedule when let the job run at a specific time:

SPSchedule schedule = SPSchedule.FromString("daily at 12:59:00");

You can also Schedule the job every one minute, every one hour, every week, something like:

SPMinuteSchedule myMinuteSchedule = new SPMinuteSchedule();
myMinuteSchedule.BeginSecond = 0;
myMinuteSchedule.EndSecond = 59;
myMinuteSchedule.Interval = 1;
myJob.Schedule = myMinuteSchedule;

No comments: