Monday, December 17, 2007

Creating custom timer jobs in SharePoint:


Creating custom timer jobs in SharePoint:

In sharepoint 2007 we can create custom timer jobs.

1. Create a new Class Library project in VS 2005.
2. Add reference of SharePoint.
3. Crate a new class, implement SPJobDefinition, with a blank constructor.
4. Overide Execute method and write the code to execute in this mehod.
5. Sign the assembly with strong name and deploy in GAC.
6. So the class structure will be like this.

---------------------------------------------------






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

namespace NDACommon
{
public class MyTimerJob : SPJobDefinition
{
public MyTimerJob ()
{
}

public MyTimerJob (string MyJobName, SPWebApplication MyWebApplication) : base(MyJobName, MyWebApplication, null, SPJobLockType.Job)
{
}

public override void Execute(Guid targetInstanceId)
{
try
{
System.IO.File.AppendAllText(@"C:\temp\Timer.txt", "SP TimerJob : " + DateTime.Now.ToString() + "\r\n");
}
catch (Exception ex)
{
System.IO.File.AppendAllText(@"C:\temp\Timer.txt", "SP TimerJob : " + DateTime.Now.ToString() + "\r\n");
}
}
}
}


---------------------------------------------------

Adding Timer Job into SharePoint

---------------------------------------------------







static void CreateJob()
{
SPServer myServer = SPServer.Local;
SPSite mySite = new SPSite("http://server/sites/musite");
SPWebApplication myApplication = mySite.WebApplication;
SPJobDefinitionCollection myJobsColl = myApplication.JobDefinitions;

NDATimerJob myJob = new NDATimerJob("myCountDocsJob", myApplication);
myJob.Title = "My Test Job";

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

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

MessageBox.Show("Job Installed");
}



---------------------------------------------------

Removing Timer Job from SharePoint

---------------------------------------------------








public static void DeleteJob()
{
SPSite mySite = new SPSite("http://server/sites/musite ");

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

MessageBox.Show("Job Deleted");
}
}
}


---------------------------------------------------

Debugging Timer Job.

Attach OSTIMER.EXE process and debug.

2 comments:

tatyaso said...

The way you created a timer job is correct but i want to SPcontext in

class library


i.e
i created a timer job its working.

i created a call library which is called by timer job.

inside the class library i want to get The way you created a timer job is correct but i want to

SPcontext.current.site

but it giving a error . Object reference any solutin

Shaiju Kumar said...

In timer job you can't get SPContext as it not running under http context, it is activating by SharePoint timer.