sharepoint timer job 读取config文件内容
来源:互联网 发布:乐动力微信数据来源 编辑:程序博客网 时间:2024/05/17 21:40
需要写一个自定义的sharepoint timer job, 目的是要定时到Site Collection Images这个List里检查图片的过期日期,如果即将过期的话,需要发送email到相关的人员。
GOOGLE了一下,发现资料很少。不过幸运的是我发现了 Andrew Connell的博客,上面有一个很详细的示例代码。http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx
代码的基本思路是:我们的自定义job类需要从SPJobDefinition继承过来,并且要改写几个构造体和一个Execute方法。其中不带参数的默认构造体是必须的。
那么在Execute方法中怎么得到这个SPWebApplication对象呢?AC用了如下的代码:
SPWebApplication webApplication = this.Parent as SPWebApplication;
用Parent属性并且CAST成WebApplication对象, 这段我比较困扰,因为根据SDK里的说明,本来就有一个WebApplication属性可以用,为什么还要这么麻烦呢。我试了如下代码
SPWebApplication webApplication = this.WebApplication;
测试结果发现完全等效。
好,这个类写完以后,我们还要写另外一个SPFeatureReceiver类。因为我们需要一个Feature来把我们的Timer部署到服务器上去,通过Feature Activated/deactivated来触发/关闭这个TimerJob。
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
SPWeb web = site.RootWeb;
// make sure the job isn't already registered
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == "ImageValidationCheckerJob")
{
job.Delete();
}
}
// install the job
ImageValidationCheckerJob imageValidationCheckerJob = new ImageValidationCheckerJob("ImageValidationCheckerJob", site.WebApplication);
SPDailySchedule schedule = new SPMonthlySchedule();
schedule.BeginHour = 23;
schedule.BeginMinute = 40;
schedule.BeginSecond = 1;
schedule.EndHour = 23;
schedule.EndMinute = 59;
schedule.EndSecond = 1;
imageValidationCheckerJob.Schedule = schedule;
imageValidationCheckerJob.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
// delete the job
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == "ImageValidationCheckerJob")
{
job.Delete();
}
}
}
这个SPDailySchedule对象让我有些困扰,我现在理解是你要给定开始时间和结束时间,然后系统会随机算出一个在两者中间的时刻来启动这个工作。比如上面例子,那么每天运行时间应该是介于23:40:1 到23:59:1 之间, 不知道是不是这样,不过我猜测应该是如此。
代码写完了以后,还要写一个Feature,这个Feature比较简单,就一个XML文件
Feature.xml:
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="1F481C17-4FDA-4919-A64A-EAE5C1301B4B"
Title="Image Validation Checker"
Description="If any images in the top level site colleciton images are expiring soon,email relative person."
Scope="Site"
Hidden="TRUE"
Version="1.0.0.0"
ReceiverAssembly="TimerJobControl, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f2aef6a9088f714f"
ReceiverClass="TimerJobControl.ImageValidationCheckerJobInstaller">
</Feature>
剩下的就是把feature装好就好了,假如代码没有出错的话。
至于怎么Debug这个程序,走以下步骤:(有些步骤有时候可能不用,但是全用可以保证没有问题,这个是我花了1天的时间得出的血的教训)
(为了调试方面,把Schedule设置为SPMinuteSchedule,并且设置为每2分钟运行一次)
把Assembly DLL放到 GAC
命令行:iisreset
先Deactivate feature, 然后activate feature.
命令行:net stop SPTimerV3
命令行:net start SPTimerV3
Visual Studio: Attach to process: OWSTIMER.EXE
完。
还有一点要注意的是,如果你想在TimerJob类里面要从web.config文件得到一些值得话,我觉得是不可能的,至少我还没有发现该怎么做,所以,假如代码需要从外部取得一些信息的话,
解决办法是 :在 C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN 目录里新建一个文件,文件名叫做:OwsTimer.exe.config,
<configuration>
<appSettings>
<add key="YourKey" value="YourValue" />
</appSettings>
</configuration>
然后用ConfigurationManager.AppSettings.Get("YourKey"); 来取得这个值。
记住每次修改代码以后进行调试前,一定要走上面说的步骤,特别是步骤4和5很容易被忘记,不然可能会遇到很莫名其妙的问题。
最后,happy SharePointING。
- sharepoint timer job 读取config文件内容
- Sharepoint Custom Timer Job
- 读取Config.ini文件内容
- 清除sharepoint timer job 缓存
- 创建SharePoint 2010 Timer Job
- 读取App.config文件中的内容
- SharePoint 2010 自定义计时器(Timer Job)
- sharepoint 2010 如何创建一个timer job
- SharePoint 2013 中开发Timer Job
- SharePoint中开发自定义Timer Job
- SharePoint Debug - SharePoint timer job 发送重复邮件
- sharepoint 2010 用timer job 实现文档下载次数
- sharepoint 2010 用timer job 实现列表评论次数
- SharePoint timer job运行时的账户问题
- config.nmake 文件内容:
- 读取.config文件
- 读取app.config文件
- C# 读取config文件
- linux boot process
- C语言嵌入式系统编程修炼
- OpenCV2.2的改动(转载)
- 牛逼VC采访牛逼创业者:Fred Wilson采访Dennis Crowley
- Chindle交互演进
- sharepoint timer job 读取config文件内容
- android中点AndroidManifest.xml和main.xml文件反编译
- 正则表达式30分钟入门教程
- SQL Server触发器创建、删除、修改、查看示例步骤
- Dom4j的使用(全而好的文章)
- 7天精通C#教程 (更新完毕)
- sql语句中的条件控制 case ..when
- 数据库 游标
- SipDroid的学习文档在以下的百度图文