- 浏览: 9399 次
最新评论
A few weeks back I posted about how you can create custom timer jobs for use in the latest release of SharePoint to perform scheduled tasks. However, considering there have been almost 40 comments to the post in the last three weeks, I figured the post needed some clarification (duh, ya think?).
The first thing you need to do is create a class that inherits from the Microsoft.SharePoint.Administration.SPJobDefinition class. To implement this class, you need to create a few constructors and override the Execute() method, like so:
1: namespace AndrewConnell.TaskLogger { 2: public class TaskLoggerJob : SPJobDefinition{ 3: 4: public TaskLoggerJob () 5: : base(){ 6: } 7: 8: public TaskLoggerJob (string jobName, SPService service, SPServer server, SPJobLockType targetType) 9: : base (jobName, service, server, targetType) { 10: } 11: 12: public TaskLoggerJob (string jobName, SPWebApplication webApplication) 13: : base (jobName, webApplication, null, SPJobLockType.ContentDatabase) { 14: this.Title = "Task Logger"; 15: } 16: 17: public override void Execute (Guid contentDbId) { 18: // get a reference to the current site collection's content database 19: SPWebApplication webApplication = this.Parent as SPWebApplication; 20: SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId]; 21: 22: // get a reference to the "Tasks" list in the RootWeb of the first site collection in the content database 23: SPList taskList = contentDb.Sites[0].RootWeb.Lists["Tasks"]; 24: 25: // create a new task, set the Title to the current day/time, and update the item 26: SPListItem newTask = taskList.Items.Add(); 27: newTask["Title"] = DateTime.Now.ToString(); 28: newTask.Update(); 29: } 30: } 31: }
As you can see, this job does nothing important but add a new item to a Task list every time it's executed. Now that you have the job built, you need to get it registered. Unfortunately the only way to do this is through code, but it sure would be one heck of a candidate for a custom STSADM command. Anyway, since we don't have that today, I like to use the feature activated & deactivated events to install/uninstall my custom jobs.
To do this, you have to create another class that inherits from the Microsoft.SharePoint.SPFeatureReceiver class and implement the FeatureActivated & FeatureDeactivated event handlers like so:
1: namespace AndrewConnell.TaskLogger { 2: class TaskLoggerJobInstaller : SPFeatureReceiver { 3: const string TASK_LOGGER_JOB_NAME = "TaskLogger"; 4: public override void FeatureInstalled (SPFeatureReceiverProperties properties) { 5: } 6: 7: public override void FeatureUninstalling (SPFeatureReceiverProperties properties) { 8: } 9: 10: public override void FeatureActivated (SPFeatureReceiverProperties properties) { 11: SPSite site = properties.Feature.Parent as SPSite; 12: 13: // make sure the job isn't already registered 14: foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) { 15: if (job.Name == TASK_LOGGER_JOB_NAME) 16: job.Delete(); 17: } 18: 19: // install the job 20: TaskLoggerJob taskLoggerJob = new TaskLoggerJob(TASK_LOGGER_JOB_NAME, site.WebApplication); 21: 22: SPMinuteSchedule schedule = new SPMinuteSchedule(); 23: schedule.BeginSecond = 0; 24: schedule.EndSecond = 59; 25: schedule.Interval = 5; 26: taskLoggerJob.Schedule = schedule; 27: 28: taskLoggerJob.Update(); 29: } 30: 31: public override void FeatureDeactivating (SPFeatureReceiverProperties properties) { 32: SPSite site = properties.Feature.Parent as SPSite; 33: 34: // delete the job 35: foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) { 36: if (job.Name == TASK_LOGGER_JOB_NAME) 37: job.Delete(); 38: } 39: } 40: } 41: }
Now... to get it working, all you need to do is:
- Deploy the strongly named assembly to the GAC.
- Reset IIS (required for SharePoint to "see" the new timer job in the GAC).
- Create a feature specifying the receiver class and assembly that contains the event receivers.
- Install the feature.
- Activate the feature.
This sample timer job assumes it's running within the context of a WSS v3 site that has a list created with the Tasks list template in the root web within the site collection (or, just create a Team Site at the root of your site collection).
Once you activate the feature, it should show up on the Timer Job Definitions page in Central Administration / Operations. It won't appear in the Timer Job Status page until it's executed at least one time. Wait for five minutes or so (the schedule this sample is using) to see if it's running. You should start to see items showing up in the Tasks list in the root site in your site collection.
Here's a Visual Studio 2005 solution that includes everything you need to create a custom timer job. Note that I used the technique I described in this article to modify the Visual Studio project to create the WSP file for me:
» TaskLogger_CustomTimerJob.zip
Or, you can use this Windows SharePoint Solution Package (*.WSP) to deploy the prebuilt timer job used in this article (handles steps 1-4 above):
» AndrewConnell.TaskLoggerJob.zip (unpack the ZIP to get the WSP)
To deploy the WSP file, simply add it to the solution store using STSADM (using the following command) and then deploy it to the desired site:
stsadm –o addsolution –filename AndrewConnell.TaskLoggerJob.wsp
Note: if you plan to test the timer job attached in that article, please make sure you also uninstall it. I just realized I never uninstalled mine and my Tasks list had over 30,000 items in it from the last few weeks. Oops! :)
[Update 7/2/2007 @ 11p] Make sure you don't use visible Features when deploying your custom timer jobs as explained here in this post. The two code downloads in this article have been updated to reflect as such.
[Update 4/8/2008 @ 12a] A Visual How-To I created on the subject of creating custom timer jobs was recently posted on MSDN. You can get more info here.
[Update 5/1/2008 @ 730a] There is now an in-depth article on MSDN about creating custom timer jobs. If you have trouble please check this out as there is a ton of additional info here. Post: More Help on Creating Custom Timer Jobs
- TaskLogger_CustomTimerJob.zip (5.7 KB)
- 下载次数: 0
- AndrewConnell.TaskLoggerJob.zip (3.9 KB)
- 下载次数: 0
发表评论
-
Hiding SharePoint list field (column) from newform and editform using powershell
2015-09-08 15:20 381$WebUrl = "http://sitecol ... -
SharePoint List View Lookup Threshold | TahoeSmart
2015-05-18 15:23 418As SharePoint admins, we typic ... -
SharePoint list lookup column indexing and CAML query relation
2015-05-18 14:56 451I’m developing the SharePoint ... -
SharePoint 2010: How to Change the List View Threshold and Other Resource Thrott
2015-05-18 14:26 336原文链接:http://blogs.msdn.com/b/ ... -
使用SharePoint Designer2010 向SharePoint2010的Ribbon中添加自定义操作
2015-05-07 16:06 589Form Name Ribbon Location ... -
客户端出错:无法在数据表视图中显示该列表: 没有安装与 Windows SharePoint Services 兼容的数据表组件
2015-04-30 11:32 1283客户端想在数据表中编辑时提示下列错误:数据视图由于下列一个或 ... -
临时禁用大型列表上的列表视图阈值
2014-12-02 21:32 485.\makeexceptionlist.ps1 -WebU ... -
SharePoint 2010在非域环境下的安装方法
2013-07-17 17:19 945完整安装完SharePoint 2010,不要运行配置向导。 ... -
HOWTO: Configuring a Office SharePoint Server 2007 Publishing Site
2012-04-19 16:06 879原文链接 This article was been ... -
SPList获得用户类型列表栏的值
2012-01-09 16:28 30原文链接 SP列表中的用户类型值,使用item[&qu ... -
Replacing SharePoint 2010 Menu Control
2012-01-06 16:54 722原文链接 March 4, 2011 by Arya ...
相关推荐
基于sharepoint timer服务 自己开发的一个sharepoint 2010 定时提醒功能。内容包括应用到的类简介,部署讲解,开发步骤和源代码。平常都在使用,所以不必考虑可行性~
Sharepoint Timer Job(邮件提醒,定时发送邮件)开发详解 文档配套源代码
Sharepoint2010、自定义定时任务、Timejob的一个插件
C语言02-Timer0-Timer1-Timer2-Timer3-Timer4测试程序(STC32G-DEMO-CODE-220311kw)C语言02-Timer0-Timer1-Timer2-Timer3-Timer4测试程序(STC32G-DEMO-CODE-220311kw)C语言02-Timer0-Timer1-Timer2-Timer3-Timer4...
最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED最新单片机仿真 TIMER0与TIMER1控制条形LED...
单片机C语言程序设计 TIMER0与TIMER1控制条形LED(有源码)单片机C语言程序设计 TIMER0与TIMER1控制条形LED(有源码)单片机C语言程序设计 TIMER0与TIMER1控制条形LED(有源码)单片机C语言程序设计 TIMER0与TIMER1控制...
sharepoint 2010 用timer job 实现列表评论次数
自定义计时器。 开发人员指令:clone npm i npm run dev
本文实例讲述了C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析,分享给大家供大家参考。具体分析如下: 在.NET Framework里面提供了三种Timer ① System.Windows.Forms.Timer ② System.Timers.Timer ③...
单片机C语言程序设计31 TIMER0与TIMER1控制条形LED(基于8051+Proteus仿真)单片机C语言程序设计31 TIMER0与TIMER1控制条形LED(基于8051+Proteus仿真)单片机C语言程序设计31 TIMER0与TIMER1控制条形LED(基于8051+...
基于Verilog的timer计时器,start开始,到达设置计时点时输出一个高电平up信号
ajax_timer ajax_timer ajax_timer ajax_timer
学习32位单片机的基础例程,主要针对初学者对TIMER的编程学习
addintimer高级版是一个万能定时器,非常好用,强烈推荐,可以当时间定时器来用,还有整点报时,定时录音等功能
加Timer控件 timer1 编写其Tick事件为 private void timer1_Tick(object sender, EventArgs e) { this.toolStripStatusLabel3.Text = "系统当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); } ...
对POSIX timer的面向对象的封装。
1.Timer Mechanism 2.Hardware Timer 3.Timer in OS 4.Timer in APP 5.Timer in Qemu
Timer Input Capture using stm8
c8051f340 TIMER0定时器中断,通过定时器达到所需的功能