SharePoint 2010 自定义计时器(Timer Job)
2011-12-23 16:32 四毛的家 阅读(1329) 评论(2) 编辑 收藏 举报
1.使用Vs2010创建一个空SharePoint 项目(我的名字:SharePointCusTimer),然后右击项目名添加一个类(我的名字:MyTimerJob.cs),打开该类修改代码如下:(代码下载https://files.cnblogs.com/sygwin/SharePointCusTimer.rar)
using System;
using Microsoft.SharePoint;//必须命名空间
using Microsoft.SharePoint.Administration;
namespace SharePointCusTimer
{
class MyTimerJob:SPJobDefinition
{
public MyTimerJob() : base() { }
public MyTimerJob(SPWebApplication webApp)
: base("MyTimerJob", webApp, null, SPJobLockType.ContentDatabase)
{
this.Title = "MyCustomTimerJob";
}
public override void Execute(Guid targetInstanceId)
{
SPWebApplication app = this.Parent as SPWebApplication;
SPList announcements = app.Sites[0].RootWeb.Lists["民政局文档库"]; // get reference of the lists
app.Sites[0].RootWeb.AllowUnsafeUpdates = true;
//计时器定时要执行的动作。在指定文本文件里内容
string Path = @"C:\test.txt";
string Strings = string.Format("列表标题{0}----{1}", announcements.Title, DateTime.Now.ToString());
System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.UTF8);
f2.WriteLine(Strings);
f2.Close();
f2.Dispose();
}
}
}
注意命名空间的引入。
2.右击Feature文件,选择添加功能,然后右击新添的Feature,选择添加事件接收器。将FeatureActivated和FeatureDeactivating里的代码修改如下:
// 取消对以下方法的注释,以便处理激活某个功能后引发的事件。
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplication webApp = null;
SPSite site = null;
//webApp = properties.Feature.Parent as SPWebApplication;//老外是这么获取的,激活时报错
site = properties.Feature.Parent as SPSite;
webApp = site.WebApplication;
//make sure the job isn't already registered
foreach (SPJobDefinition job in webApp.JobDefinitions)
if (job.Name == "MyTimerJob") job.Delete();
MyTimerJob myJob = new MyTimerJob(webApp);
SPMinuteSchedule schedule1 = new SPMinuteSchedule();//这里是按分钟执行,具体参数说明参考msdn
schedule1.BeginSecond = 0;
//schedule1.EndSecond = 10;
schedule1.Interval = 1;
myJob.Schedule = schedule1;
myJob.Update();
});
}
// 取消对以下方法的注释,以便处理在停用某个功能前引发的事件。
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = null;
SPSite site = null;
site = properties.Feature.Parent as SPSite;
webApp = site.WebApplication;
foreach (SPJobDefinition job in webApp.JobDefinitions)
if (job.Name == "MyTimerJob") job.Delete();
}
然后右击项目名称部署,即可。
说明:1,注意相关命名空间的引入。
2.在Feature事件接收器里,注意SPSecurity.RunWithElevatedPrivileges的使用,不然job.Update()时提示拒绝访问。
3.注意SPWebApplication对象的获取。如果部署时提示未将引用对象设置到xxx,那很有可能是webapp对象是null;
4.注意SPMinuteSchedule的使用。参考http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.spdailyschedule_members.aspx
如果你想在特定的时间点执行,请使用FromString(),例如
SPSchedule schedule = SPSchedule.FromString("daily at 15:25:00"); // executes at 3:25 pm local time
5.注意,更改Feature的部署范围(我更改了feature的名字,并设置部署范围为Site)
参考:http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx
http://doitwithsharepoint.blogspot.com/2011/01/create-custom-timer-job-in-sharepoint.html
关于如何调试Feature事件接收器。
在vs里部署,默认会自动激活,可是如果使用这个默认配置,那么自己设置的断点是无法捕获到的,原因很简单。激活事件发生在断点捕获之前。
所以,我们就需要将其配置为安装不激活,然后手动激活,这样断点就可以捕获到了。做法很简单,右击项目名称,选择属性,找到SharePoint选项卡
在活动部署配置下拉框,将Default改成No Activation即可。这样部署就不会激活feature,需要手动激活了。
源码下载:https://files.cnblogs.com/sygwin/SharePointCusTimer.rar
更多资料参考:
http://blog.sharepointupshot.com/posts/category/developmentguides/403/
http://www.dotnetspark.com/kb/4116-custom-timer-job-sharepoint-2010.aspx
http://www.c-sharpcorner.com/uploadfile/shyjumohan/sharepoint-2010-creating-custom-timer-job/
http://www.wictorwilen.se/Post/Timer-job-changes-and-additions-in-SharePoint-2010.aspx
http://msdn.microsoft.com/en-us/library/hh528519.aspx
(国外网站打不开,自己想办法)