环境:Windows Server 2012,SharePoint 2013,Visual Studio 2013
定义一个Job,执行我们的操作:
创建一个class,让它继承SPJobDefinition,然后重写HasAdditionalUpdateAccess和Execute,其中Execute是执行我们自定义程序的方法。继承SPJobDefinition需要一个无参的构造函数,另外我们需要另外一个构造函数设置Job的Title和WebApplication
using Microsoft.SharePoint.Administration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JobTest.TimerJob
{
public class TestJob : SPJobDefinition
{
protected override bool HasAdditionalUpdateAccess()
{
return true;
}
public TestJob() : base() { }
public TestJob(string jobName, SPWebApplication webApp) : base(jobName, webApp, null, SPJobLockType.Job) { this.Title = jobName; }
public override void Execute(Guid targetInstanceId)
{
//跑定时器需要执行的代码
}
}
}
添加Feature,添加Feature的事件接收器,然后编辑激活和停用事件,把Job加上去就可以了。其中,下面是两种不同的TimerJob,SPDailySchedule 每天执行一次,SPMinuteSchedule 每隔N分钟执行一次。代码如下:
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace JobTest.Features.TestJob
{
/// <summary>
/// 此类用于处理在激活、停用、安装、卸载和升级功能的过程中引发的事件。
/// </summary>
/// <remarks>
/// 附加到此类的 GUID 可能会在打包期间使用,不应进行修改。
/// </remarks>
[Guid("8c2a6553-0f7f-443c-bc9b-a3e11225af9a")]
public class TestJobEventReceiver : SPFeatureReceiver
{
//定义Job的名称
const string EveryDayJobName = "EveryDayTimerJob";//每天执行的Job
const string EveryNMinutesJobName = "EveryNMinutesTimerJob";//每隔N分钟执行的Job
// 取消对以下方法的注释,以便处理激活某个功能后引发的事件。
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Title == EveryDayJobName)
job.Delete();
if (job.Title == EveryNMinutesJobName)
job.Delete();
}
TimerJob.TestJob timerjob = new TimerJob.TestJob(EveryDayJobName, site.WebApplication);//实例化任务类
//设置计时器属性,这个可以发布后在sharepoint管理器中的"监控"中重新定义。
//SPDailySchedule 每天执行的定时器
SPDailySchedule schedule = new SPDailySchedule();
schedule.BeginHour = 0;
schedule.BeginMinute = 0;
schedule.BeginSecond = 0;
schedule.EndHour = 2;
schedule.EndMinute = 0;
schedule.EndSecond = 0;
timerjob.Schedule = schedule;
timerjob.Update();
TimerJob.TestJob cstimerjob = new TimerJob.TestJob(EveryNMinutesJobName, site.WebApplication);//实例化任务类
//SPMinuteSchedule 每N分钟执行的定时器
SPMinuteSchedule csschedule = new SPMinuteSchedule();
csschedule.BeginSecond = 0;
csschedule.EndSecond = 59;
csschedule.Interval = 4;
cstimerjob.Schedule = csschedule;
cstimerjob.Update();
}
// 取消对以下方法的注释,以便处理在停用某个功能前引发的事件。
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Title == EveryDayJobName)
job.Delete();
if (job.Title == EveryNMinutesJobName)
job.Delete();
}
}
// 取消对以下方法的注释,以便处理在安装某个功能后引发的事件。
//public override void FeatureInstalled(SPFeatureReceiverProperties properties)
//{
//}
// 取消对以下方法的注释,以便处理在卸载某个功能前引发的事件。
//public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
//{
//}
// 取消对以下方法的注释,以便处理在升级某个功能时引发的事件。
//public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
//{
//}
}
}
部署完后,到Site Collection Features下面激活Feature就可以了。
可以在 管理中心→监控→计时器作业(复查作业定义) 下面查看部署好的Job。
关于调试,参照下图