How to Develop a Custom Timer Job in SharePoint
2010 using Visual Studio 2010
2. Deploy as
Farm Solution
3. Add new Class, its name TimerJobClass.cs
4. Inherit class from Microsoft.SharePoint.Administration.SPJobDefinition class. And code as
//
-----------------------------------------------------------------------
//
<copyright file="TimerJobClass.cs" company="">
// TODO:
Update copyright text.
//
</copyright>
//
-----------------------------------------------------------------------
namespace
TimerJob2010
{
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Microsoft.SharePoint.Administration;
using
Microsoft.SharePoint;
/// <summary>
/// TODO: Update summary.
/// </summary>
public class TimerJobClass:SPJobDefinition
{
public
TimerJobClass():base()
{
}
public
TimerJobClass(string jobName, SPService service, SPServer
server, SPJobLockType targetType)
: base(jobName,
service, server, targetType)
{
this.Title
= "List Timer Job 2010";
}
public
TimerJobClass(string jobName, SPWebApplication webapp)
: base(jobName,
webapp, null, SPJobLockType.ContentDatabase)
{
this.Title
= "List Timer Job 2010";
}
public override void
Execute(Guid contentDBId)
{
//get a
reference to the current site collection's content database
SPWebApplication
webApplication = this.Parent as SPWebApplication;
SPContentDatabase
contentDB = webApplication.ContentDatabases[contentDBId];
//Geta
reference to the "Student" list in the rootweb of the first site
collection in the Content DB
SPList
listJob = contentDB.Sites[0].RootWeb.Lists["Student"];
//Create
a new list item and update the item
SPListItem
newList = listJob.Items.Add();
newList["Title"]
= "32806";
newList["Firstname"]
= "Siddartha";
newList["Lastname"]
= "Reddivari";
newList["Gender"]
= "Male";
newList["Address"]
= "Bangalore";
newList.Update();
}
}
}
5: Right click on Feartures | Add
Feature
Step 6: You can rename Feature as
7: Right click on Feartures | Add
Event Receiver
8. Code as
using
System;
using
System.Runtime.InteropServices;
using
System.Security.Permissions;
using
Microsoft.SharePoint;
using
Microsoft.SharePoint.Security;
using
Microsoft.SharePoint.Administration;
namespace
TimerJob2010.Features.TimerJobFeature
{
/// <summary>
/// This class handles events raised during feature
activation, deactivation, installation, uninstallation, and upgrade.
/// </summary>
/// <remarks>
/// The GUID attached to this class may be used during
packaging and should not be modified.
/// </remarks>
[Guid("43bb3273-ef57-4bc3-bff1-52d8679f74c8")]
public class TimerJobFeatureEventReceiver
: SPFeatureReceiver
{
const string ListTimerJob = "ListTimerJob";
// Uncomment
the method below to handle the event raised after a feature has been activated.
public override void
FeatureActivated(SPFeatureReceiverProperties
properties)
{
SPSite
site = properties.Feature.Parent as SPSite;
//make
sure that the job is not already registered
foreach
(SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if
(job.Name == ListTimerJob)
job.Delete();
}
//Install
the Job
TimerJobClass
listLoggerJob = new TimerJobClass(ListTimerJob,
site.WebApplication);
SPMinuteSchedule
schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 5;
listLoggerJob.Schedule = schedule;
listLoggerJob.Update();
}
// Uncomment
the method below to handle the event raised before a feature is deactivated.
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.Title==ListTimerJob)
job.Delete();
}
}
}
}
Step 9: Right click Feature | View
Designer and change the scope to "Site"
Step 10: Build project and Deploy it.
Step 11: Go to Central Admin | Check
job status. custom timer job created, click to timer job and configure
Open
your web application | Open your list and see item was created








No comments:
Post a Comment