1,主要代码,创建一个TimeJob类,里面继承他需要继承的类,都是一个模式。
using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; using Microsoft.SharePoint.Utilities; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespace NuctechProject { public class MyTimerJob : SPJobDefinition { // 该构造只用于序列化和反列化,因为timer job的Definition 初始化后成为instance需要保存在数据库中 public MyTimerJob(): base() { } // 创建针对SPService application的timer job 是调用的构造 public MyTimerJob(string jobName, SPService service, SPServer server, SPJobLockType lockType) : base(jobName, service, server, lockType) { this.Title = "PlanWarn Timer Job";//此处是TimeJob的名称
} // 创建针对web application的timer job 是调用的构造 public MyTimerJob(string jobName, SPWebApplication webapp) : base(jobName, webapp, null, SPJobLockType.ContentDatabase) { this.Title = "PlanWarn Timer Job";//此处是TimeJob的名称
} // 每次执行到Timer Job时要执行的方法。跟据Timer Job 的Schedule public override void Execute(Guid targetInstanceId) { SPWebApplication webapp = this.Parent as SPWebApplication; SPContentDatabase contentDb = webapp.ContentDatabases[targetInstanceId]; // 取参数,通过Properties可以从外部传递参数,但要求参数可以被序列化和反列化 //需要执行的代码 }}
需要注意点:
1,this.Title = "PlanWarn Timer Job";//此处是TimeJob的名称
this.Title是设置你的TimeJob名字,到后面设置定时的时候需要找的
2,在public override void Execute(Guid targetInstanceId)里面写你的逻辑代码就好了
2,主要代码有了,现在创建一个Feature,以下代码均是自动生成。需要改的地方为
1,const string JobName = "PlanWarn Timer Job"; 此处的名字就是第一步的this.Title
2,修改FeatureActivated方法里面的new SPSite(http://demo1); 构造参数改成你配置的地址
using System; using System.Runtime.InteropServices; using System.Security.Permissions; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; namespace NuctechProject.Features.Feature1 { /// <summary> /// 此类用于处理在激活、停用、安装、卸载和升级功能的过程中引发的事件。 /// </summary> /// <remarks> /// 附加到此类的 GUID 可能会在打包期间使用,不应进行修改。 /// </remarks> [Guid("673a25ed-2556-42e5-ac1c-155ef01ae7d7")] public class Feature1EventReceiver : SPFeatureReceiver { // 取消对以下方法的注释,以便处理激活某个功能后引发的事件。 const string JobName = "PlanWarn Timer Job"; public override void FeatureActivated(SPFeatureReceiverProperties properties) { // 注意此处as 后面是SPSite还是SPWeb, SPFarm ...要根据Feature 的Scope SPSite site = new SPSite("http://demo1"); DeleteJob(site); // Delete Job if already Exists CreateJob(site); // Create new Job } private static void DeleteJob(SPSite site) { foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) if (job.Name == JobName) job.Delete(); } private static void CreateJob(SPSite site) { // 初始化 SPJobDefinition MyTimerJob job = new MyTimerJob(JobName, site.WebApplication); // 传递参数 job.Properties.Add("SiteUrl", site.Url); // 设置 schedule, 可选 SPDailySchedule, SPWeeklySchedule, SPYearlySchedule,SPMonthlyByDaySchedule,SPHourlySchedule,SPMinuteSchedule SPMinuteSchedule schedule = new SPMinuteSchedule(); schedule.BeginSecond = 0; schedule.EndSecond = 5; schedule.Interval = 5; job.Schedule = schedule; job.Update(); } // 取消对以下方法的注释,以便处理在停用某个功能前引发的事件。 public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { // 删除 Timer Job的实例 DeleteJob(properties.Feature.Parent as SPSite); // Delete the Job } // 取消对以下方法的注释,以便处理在安装某个功能后引发的事件。 //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) //{ //} } }
3,上面两步完成之后,就需要去作业定义里面设置,这个代码每隔多久执行一次了。
具体操作步骤请看这个
http://www.cnblogs.com/jianyus/p/3458535.html
我的是主要代码,上面的地址是每步骤,以及详细解释
SharePoint TimeJob使用笔记,布布扣,bubuko.com
原文:http://blog.csdn.net/qq873113580/article/details/22300645