Custom Timer Job In 2010-
I am going to illustrate how to create Custom Timer job in SharePoint 2010.
Task: Updating the Sql Server Database table after each minute from SharePoint List.
1. Create a SharePoint Project as a form Solution.
2. Take a class file "SpeedTimer.cs"
3. Copy and paste the following code:
-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using System.Configuration;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace SharePointProject2
{
class SpeedTimer : SPJobDefinition
{
public SpeedTimer()
: base()
{
}
public SpeedTimer(string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base(jobName, service, server, targetType)
{
}
public SpeedTimer(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = "Speed List Timer Job";
}
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];
// get a reference to the "ListTimerJob" list in the RootWeb of the first site collection in the content database
SPList Listjob = contentDb.Sites[0].RootWeb.Lists["BackupTimerjobList"];
//SPListItem newList = Listjob.Items.Add();
//newList["Title"] = DateTime.Now.ToString();
//newList.Update();
//Fetch Configuration file
Configuration config = WebConfigurationManager.OpenWebConfiguration("/", webApplication.Name);
//// Read Connectin string
string ConnectDB = config.ConnectionStrings.ConnectionStrings["ConnectDB"].ToString();
SqlConnection con = new SqlConnection(ConnectDB);
con.Open();
foreach (SPListItem item in Listjob.Items)
{
string str1 = "insert into listbackup (Title) values('" + item["Title"] + "')";
SqlCommand cmd = new SqlCommand(str1, con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
con.Close();
// create a new list Item, set the Title to the current day/time, and update the item
}
}
}
4. Right click on feature and click on Add Feature.
5. Select Scope as Site.
6. Now Right Click on your Feature and click on add event receiver.
7. Copy and paste the following code.
----------------------------------------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
namespace SharePointProject2.Features.Feature1
{
/// <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("2e8afeab-7a33-4da0-8a01-ab3e462f10b6")]
public class Feature1EventReceiver : SPFeatureReceiver
{
const string List_JOB_NAME = "ListLogger";
// 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 the job isn't already registered
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == List_JOB_NAME)
job.Delete();
}
// install the job
SpeedTimer listLoggerJob = new SpeedTimer(List_JOB_NAME, site.WebApplication);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 1;
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.Name == List_JOB_NAME)
job.Delete();
}
}
// Uncomment the method below to handle the event raised after a feature has been installed.
//public override void FeatureInstalled(SPFeatureReceiverProperties properties)
//{
//}
// Uncomment the method below to handle the event raised before a feature is uninstalled.
//public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
//{
//}
// Uncomment the method below to handle the event raised when a feature is upgrading.
//public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
//{
//}
}
}
8. Now Deploy your code.
I am going to illustrate how to create Custom Timer job in SharePoint 2010.
Task: Updating the Sql Server Database table after each minute from SharePoint List.
1. Create a SharePoint Project as a form Solution.
2. Take a class file "SpeedTimer.cs"
3. Copy and paste the following code:
-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using System.Configuration;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace SharePointProject2
{
class SpeedTimer : SPJobDefinition
{
public SpeedTimer()
: base()
{
}
public SpeedTimer(string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base(jobName, service, server, targetType)
{
}
public SpeedTimer(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = "Speed List Timer Job";
}
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];
// get a reference to the "ListTimerJob" list in the RootWeb of the first site collection in the content database
SPList Listjob = contentDb.Sites[0].RootWeb.Lists["BackupTimerjobList"];
//SPListItem newList = Listjob.Items.Add();
//newList["Title"] = DateTime.Now.ToString();
//newList.Update();
//Fetch Configuration file
Configuration config = WebConfigurationManager.OpenWebConfiguration("/", webApplication.Name);
//// Read Connectin string
string ConnectDB = config.ConnectionStrings.ConnectionStrings["ConnectDB"].ToString();
SqlConnection con = new SqlConnection(ConnectDB);
con.Open();
foreach (SPListItem item in Listjob.Items)
{
string str1 = "insert into listbackup (Title) values('" + item["Title"] + "')";
SqlCommand cmd = new SqlCommand(str1, con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
con.Close();
// create a new list Item, set the Title to the current day/time, and update the item
}
}
}
4. Right click on feature and click on Add Feature.
5. Select Scope as Site.
6. Now Right Click on your Feature and click on add event receiver.
7. Copy and paste the following code.
----------------------------------------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
namespace SharePointProject2.Features.Feature1
{
/// <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("2e8afeab-7a33-4da0-8a01-ab3e462f10b6")]
public class Feature1EventReceiver : SPFeatureReceiver
{
const string List_JOB_NAME = "ListLogger";
// 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 the job isn't already registered
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == List_JOB_NAME)
job.Delete();
}
// install the job
SpeedTimer listLoggerJob = new SpeedTimer(List_JOB_NAME, site.WebApplication);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 1;
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.Name == List_JOB_NAME)
job.Delete();
}
}
// Uncomment the method below to handle the event raised after a feature has been installed.
//public override void FeatureInstalled(SPFeatureReceiverProperties properties)
//{
//}
// Uncomment the method below to handle the event raised before a feature is uninstalled.
//public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
//{
//}
// Uncomment the method below to handle the event raised when a feature is upgrading.
//public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
//{
//}
}
}
8. Now Deploy your code.
0 comments:
Post a Comment