项目需要写TimerJob,以前也大概知道原理,不过,开发过程中,还是遇到一些问题,网上看了好多博客,也有写的灰常好的,不过,自己还是想再写一下,也算是给自己一个总结,也算给大家多一个参考吧。

TimerJob项目结构,主要有两个Class,一个是用来定义TimerJob功能的,一个是用来部署开发好的TimerJob的,分别继承两个不同的类。如下图,先建一个如下结构的项目:

文件描述:

TimerJob定义类:ModifyTitle.cs(继承自SPJobDefinition)

TimerJob安装类:ModifyTitleInstall.cs(继承自SPFeatureReceiver)

激活TimerJob的Feature.xml

添加强命名,因为将来生成的dll是要放到GAC里面去的

添加引用:

引用Microsoft.SharePoint.dll文件,两个Class都需要添加下面命名空间

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

ModifyTitleInstall类

public class ModifyTitleInstall : SPFeatureReceiver

{

const string TimerJobName = "ModifyTitleTimerJob";//TimerJob的标题

//激活TimerJob的方法

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

SPSite site = properties.Feature.Parent as SPSite;

foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)

{

//如果有相同的TimerJob,先删除

if (job.Title == TimerJobName)

{

job.Delete();

}

}

ModifyTitle modifyTitle = new ModifyTitle(TimerJobName, site.WebApplication);

SPMinuteSchedule minuteSchedule = new SPMinuteSchedule();//计时器对象

minuteSchedule.BeginSecond = 0;

minuteSchedule.EndSecond = 59;

minuteSchedule.Interval = 1;

modifyTitle.Schedule = minuteSchedule;

modifyTitle.Update();

//throw new NotImplementedException();

}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

{

SPSite site = properties.Feature.Parent as SPSite;

foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)

{

if (job.Title == TimerJobName)

{

job.Delete();

}

}

//throw new NotImplementedException();

}

public override void FeatureInstalled(SPFeatureReceiverProperties properties)

{

//throw new NotImplementedException();

}

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)

{

//throw new NotImplementedException();

}

ModifyTitle类

public class ModifyTitle : SPJobDefinition

{

public ModifyTitle():base(){}

public ModifyTitle(string TimerName, SPWebApplication webapp) : base(TimerName, webapp, null, SPJobLockType.ContentDatabase)

{

//TimerJob的标题

this.Title = "定期修改Title的TimerJob";

}

public override void Execute(Guid targetInstanceId)

{

SPWebApplication webapp = this.Parent as SPWebApplication;

SPContentDatabase contentDB=webapp.ContentDatabases[targetInstanceId];

foreach (SPItem item in contentDB.Sites[0].RootWeb.Lists["TimerJob"].Items)

{

DateTime dt = Convert.ToDateTime(item["创建时间"].ToString());

item["标题"] = "今天是这个月的第" + dt.Day.ToString() + "天";

item.Update();

}

//base.Execute(targetInstanceId);

}

}

Feature.xml(Id是需要重新生成的Guid)

<?xml version="1.0" encoding="utf-8" ?>

<Feature xmlns="http://schemas.microsoft.com/sharepoint/"

Id="f0c813e8-68e0-4ad2-82cd-292b1b7222cd"

Title="Modify Title Timer Job"

Description="Modify Title Timer Job"

Scope="Site"

Hidden="TRUE"

Version="1.0.0.0"

ReceiverAssembly="TimerJob, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f7436af6afb9480b"

ReceiverClass="TimerJob.ModifyTitleInstall">

</Feature>

添加结果:

运行结果:无论标题是什么,都改成今天是这个月的第N天。

添加配置文件:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

<add key="AAString" value="http://localhost"/>

</appSettings>

</configuration>

获取配置文件:

string AAString = ConfigurationManager.AppSettings.Get("AAString");

注:配置文件格式不对的话,可能造成Timer服务启动错误,所以,可以拷一个控制台程序debug下面的Consoleapp.exe.config文件,然后改成OWSTIMER.exe.config,然后放到12/bin(C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\BIN)下就可以了

部署TimerJob脚本:

@echo off

SET TEMPLATE="c:\program files\common files\microsoft shared\web server extensions\12\Template"

Echo Copying files to TEMPLATES directory

xcopy /e /y 12\TEMPLATE\* %TEMPLATE%

Echo Copying TimerJob.dll to GAC

"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\gacutil.exe" -if bin\TimerJob.dll

iisreset

"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\bin\stsadm" -o installfeature -filename TimerJob\feature.xml -force

"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\bin\stsadm" -o deactivatefeature -filename TimerJob\feature.xml -url http://localhost -force

"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\bin\stsadm" -o activatefeature -filename TimerJob\feature.xml -url http://localhost -force

net stop SPTimerV3

net start SPTimerV3

PAUSE

注:新的TimerJob运行一定要重启SPTimerV3服务,在windows服务里面,如下图:

调试:TimerJob程序和WebPart等SharePoint程序,运行的进程不一样,如果需要调试,需要重新安装TimerJob,然后附加到SharePoint计时器进程(下图),进行调试!

体会:

开发完TimerJob感觉,和SharePoint的东西有一样的特点,就是代码开发比较简单,但是杂七杂八的事情很多,部署、调试起来比较麻烦,而且非常需要细心,如果其间遇到各种bug,可以建议重启下机器(我就是头天晚上,各种报错,转天就好了)。

还有就是,我的代码是SharePoint2007环境开发的,如果在2010或者更高版本,代码基本是类似的,注意目录即可,部署方式可能需要PowerShell,可以网上查一下。

SharePoint 开发TimerJob 介绍的更多相关文章

  1. SharePoint开发 - TimerJob简单实例讲解

    博客地址 http://blog.csdn.net/foxdave SharePoint中的TimerJob类似于Windows系统的计划任务,可以实现定时执行指定操作的功能. 本篇所述的实例为在Sh ...

  2. SharePoint Server 2013开发之旅(一):新的开发平台和典型开发场景介绍

    我终于开始写这个系列文章,实际上确实有一段时间没有动笔了.最近重新安装了一套SharePoint Server 2013的环境,计划利用工作之余的时间为大家写一点新的东西. SharePoint Se ...

  3. [转]SharePoint开发中可能用到的各种Context(上下文)

    SharePoint是一个B/S结构的产品,所以在开发过程中会使用到各种各样的上下文(Context)信息,借此机会来总结一下.特别是Javascript的Ctx非常实用,这里记录一下! 一.Http ...

  4. SharePoint开发中可能用到的各种Context(上下文)

    转载: http://www.cnblogs.com/erucy/archive/2012/08/25/2655600.html 电脑正在以无比慢的速度从微软网站上安装Office Component ...

  5. SharePoint Add-in Model 介绍 - 引文(先导篇)

    1. SharePoint 平台 如果你已经很熟悉 SharePoint 平台,可跳过本章节. 1.1 SharePoint 是什么 在介绍 Add-in Model 之前,简要提一下 SharePo ...

  6. SharePoint常用目录介绍

    SharePoint常用目录介绍 stsadm命令管理程序目录:C:\Program Files\Common Files\Microsoft Shared\web server extensions ...

  7. cWeb开发框架,基于asp.net的cWeb应用开发平台介绍(二)

    cWeb是基于微软的.Net Framework 4框架,数据库是sql server 2008 r2. cWeb开发框架下载,点击这里去下载. cWeb开发框架借鉴三层架构理论分为三层,分别是:cD ...

  8. cWeb开发框架,基于asp.net的cWeb应用开发平台介绍(一)

    cWeb开发框架是基于asp.net的B/S应用开发平台,采用三层架构理论,应用简单.代码简洁.运行快速. cWeb是bubufx提供,是分享资源,无任何版权限制,bubufx继续传承互联网精神,可随 ...

  9. [推荐]WebService开发知识介绍

    [推荐]WebService开发知识介绍 WebService开发手册  http://wenku.baidu.com/view/df3992ce050876323112128a.html WebSe ...

随机推荐

  1. 菜鸟学习物联网---辨析基于Andriod 5.1,Linux,Windows10开发Dragon Board 410c板

    点击打开链接 诸位亲最近怎么样?刚过完年上班是不是很不情愿?自古做事者,不唯有坚韧不拔之志,亦或有超世之才.所以,诸位好好加油.今天小编想给大家系统性总结一下Dragon Board 410c板基于A ...

  2. 6、Android Content Provider测试

    如果你的应用中使用了Content Provider来与其他应用进行数据交互,你需要对Content Provider进行测试来确保正常工作. 创建Content Provider整合测试 在Andr ...

  3. Cocos2D实现上下滚动式状态窗口

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 有时候要显示的内容太多,我们无法在iOS设备的小屏幕上显示出来 ...

  4. 【Netty源码学习】EventLoopGroup

    在上一篇博客[Netty源码解析]入门示例中我们介绍了一个Netty入门的示例代码,接下来的博客我们会分析一下整个demo工程运行过程的运行机制. 无论在Netty应用的客户端还是服务端都首先会初始化 ...

  5. linux qcom LCD framwork

    点击打开链接 0.关键字 MDSS : Multimedia Display sub system DSI: Display Serial Interface 1.涉及文件 (1) drivers\v ...

  6. UNIX网络编程——UDP缺乏流量控制(改进版)

    现在我们查看无任何流量控制的UDP对数据报传输的影响.首先我们把dg_cli函数修改为发送固定数目的数据报,并不再从标准输入读.如下,它写2000个1400字节大小的UDP数据报给服务器. 客户端程序 ...

  7. 【C++知识点】单例模式的简单实现

    单例模式是最常见,也是使用最广泛的一种设计模式,其意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享. 单例模式的实现方法有很多种,本文只给出一个最简单的实现,如下: ...

  8. 解决android 大图OOM的两种方法

    最近做程序中,需要用到一张大图.这张图片是2880*2180大小的,在我开发所用的华为3C手机上显示没有问题,但是给米3装的时候,一打开马上报OOM错误.给nexus5装,则是图片无法出来,DDMS中 ...

  9. 15-5-23 下午02时22分58秒 CST> <Info> <Management> <BEA-141281> <unable to get file lock, will retry ...>

     A-141281> <unable to get file lock, will retry ...>   http://gdutlzh.blog.163.com/blog/s ...

  10. Linux编译Windows共享目录下代码

    Linux编译Windows共享目录下代码(金庆的专栏)万神服务器代码是跨平台的.平时策划在Windows上开自己的服务器测试,测试和发布服务器为Linux.开发时,先在Windows上编译测试,再到 ...