这方面的文章园子里有几篇了,不过大家基本上都是参考了http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx这篇文章,在此我也推荐大家仔细看下本文及文中涉及到的相关文章。工作中也遇到了类似的需求,在此也做个总结。
1.MOSS中已经提供了定时器的功能,我们要开发自己的定时器需要继承自SPJobDefinition类,在重写的Execute方法写自己的业务逻辑。我的需求是找出列表中符合条件的item,并做mail通知,代码如下:
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint.Administration; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; namespace CaryTimer { public class ListRemindEvent : SPJobDefinition { public ListRemindEvent() : base() { } public ListRemindEvent(string _timername, SPWebApplication _wp) : base(_timername, _wp, null, SPJobLockType.ContentDatabase) { this.Title = "TestTimer"; } public override void Execute(Guid targetInstanceId) { SPWebApplication webApp = this.Parent as SPWebApplication; SPContentDatabase contentDB = webApp.ContentDatabases[targetInstanceId]; SPWeb web = contentDB.Sites[0].AllWebs[0]; string sendTo = ""; string mailTitle = ""; string mailBody = ""; //实现自己业务逻辑,找出复合条件的并发mail做相关通知。 SPUtility.SendEmail(web, false, false, sendTo, mailTitle, mailBody); } } }
2.该类完成后我们使用Feature来部署该功能,我们写一个自己的安装类,如下:
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; namespace CaryTimer { public class ListRemindEventInstaller : SPFeatureReceiver { const string caryTimerName = "Testtimer"; public override void FeatureInstalled(SPFeatureReceiverProperties properties) { } public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { } public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPSite site = properties.Feature.Parent as SPSite; foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) { //判断是否已存在 if (job.Name == caryTimerName) { job.Delete(); } } ListRemindEvent timer = new ListRemindEvent(caryTimerName, site.WebApplication); /下边是设置定时器的执行计划部分。 SPMinuteSchedule schedule = new SPMinuteSchedule(); schedule.BeginSecond = 0; schedule.EndSecond = 59; schedule.Interval = 1; timer.Schedule = schedule; timer.Update(); } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { SPSite site = properties.Feature.Parent as SPSite; foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) { if (job.Name == caryTimerName) { job.Delete(); } } } } }
3.需要在"c:\program files\common files\microsoft shared\web server extensions\12\Template\features"文件夹下建立CaryTimer文件夹,在该文件夹下建一个Feature.xml,代码如下:
<Feature Id="6283ADA0-B882-47fe-8507-D8CC763DC320" Title="CaryTimer" Description=" CaryTimer des" Scope="Site" Hidden="FALSE" ReceiverAssembly="HelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b38a04419cc857d9" ReceiverClass="HelloWorld.ListRemindEventInstaller" xmlns="http://schemas.microsoft.com/sharepoint/"> </Feature>
4.然后我们使用一个批处理来安装Feature,批处理代码如下:
@SET TEMPLATEDIR="c:\program files\common files\microsoft shared\web server extensions\12\Template"
@SET STSADM="c:\program files\common files\microsoft shared\web server extensions\12\bin\stsadm"
@SET GACUTIL="d:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe"
Echo Installing HelloWorld.dll in GAC
%GACUTIL% -if bin\debug\HelloWorld.dll
Echo Copying files to TEMPLATE directory
xcopy /e /y TEMPLATE\* %TEMPLATEDIR%
Echo Installing feature
%STSADM% -o installfeature -filename HelloWorld\feature.xml -force
IISRESET
REM cscript c:\windows\system32\iisapp.vbs /a "SharePointDefaultAppPool" /r
5.项目完成后,我们要添加强命名密钥。部署成功后就可以在网站集功能中看见该Feature,激活该Feature后,在管理中心—操作—计时器作业定义中可以看见该定时器的相关信息,并且可以禁用和启用该定时器,在管理中心—操作—计时器作业状态中可以该定时器最后一次运行的情况。
6.如果我们要调试该定时器我们需要附加OwsTimer.exe进程,每次更改后需走以下步骤:
6.1.使用批处理从新部署Feature
6.2.先Deactivate feature, 然后activate feature.
6.3.命令行:net stop SPTimerV3
6.4.命令行:net start SPTimerV3
6.5.Visual Studio: Attach to process: OWSTIMER.EXE
7.如果你在ListRemindEvent类中需要读取外部文件的配置,直接读web.config是读不到的,我们需要在c:/program files/common files/microsoft shared/…/12/bin目录里新建一个文件OwsTimer.exe.config,并做相关配置如下:
<configuration> <appSettings> <add key="key" value="value" /> </appSettings> </configuration> 然后用ConfigurationManager.AppSettings.Get("key"); 来取得这个值。
发表评论
-
Sharepoint之升级篇
2008-03-07 07:47 1560一:sharepoint介绍<?xml:namespac ... -
SharePoint之定义管理路径
2008-03-12 07:14 1022一:如何设置 通过定义管理路径,可以指定 Web 应用程序 ... -
Sharepiont之页面模型
2008-03-15 17:33 1024本文主要包括以下几方面内容:1.页面布局和母版页2.页面布局模 ... -
Sharepoint站点把我的名字显示成“系统帐户”问题
2008-03-18 21:48 951在你安装了SharePoint Server 2007之后,当 ... -
MOSS中对列表的一些操作(创建,查询等)
2008-06-17 20:47 8541.查询列表的所有字段 SPSite site = new S ... -
使用对象模型来创建内容类型
2008-07-03 21:49 601一:首先定义一个函数来创建内容类型,代码如下: private ... -
微软发布WSS3.0和MOSS2007 基础结构更新
2008-07-17 07:07 683一:Windows SharePoint Services 3 ... -
[翻译]Training Site Template—Part 1:介绍
2008-07-30 20:08 655原文地址:http://blogs.msdn.com/shar ... -
如何取到MOSS列表中item的链接
2008-12-04 18:07 778SPListItem的Url属性返回的不是绝对路径,形式如下面 ... -
MOSS程序中如何发Mail?
2008-12-09 10:42 755我们使用.NET类库中的API发Mail的时候,我们要配置他的 ...
相关推荐
MOSS中文SDK MOSS中文SDK MOSS中文SDK MOSS中文SDK
MOSS中配置SSL安全设置 moss ssl https 安全 asp.net sharepoint2007
在MOSS中配置RMS手册(英文).doc
moss中使用第三方邮箱解决方案,请大家多指教
moss中eventHandler使用,详细介绍了在moss中eventhandler使用的方式
moss 中修改列表上传图片的方法,有源代码和操作方法说明。
实现图片自动上传,很方便的MOSS程序!
目前总结出两种简单的方法,都是用Moss本身和Excel进行操作,无需其他任何工具。一种是最基本的复制粘贴,第二种是利用Excel和Moss中的数据导入功能
moss 2007开发教程,moss 2007开发教程(1).MOSS2007之概述
MOSS QueryTool Stramit.CamlViewer2007
MOSS基础教程.doc
MOSS调查多选分析器
Moss 2007 2010 面试题 interview
MOSS2007的定制MOSS2007的定制
在MOSS中开发和部署Infopath 在InfoPath中同时上传多个附件
moss环境搭建,从前期准备,到AD搭建,到安装.net framework,到安装数据库,到安装moss server,到配置,一应俱全。
sharepoint 2007 moss
说明:MOSS 默认会采用NT Authority\NetworkService 账户配置搜索相关服务及爬网账户, 这经常会导致权限问题,而搜索功能不可用;...般可解决问题。 (可专门创建针对MOSS 应用的账户,便于管理。)