项目需要写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开发中可能用到的各种Context(上下文)

    第一部分 服务器端上下文 一.HttpContext 这个--我想就不用再介绍了,SharePoint运行在标准的ASP.NET框架下(2003用的不是标准的ASP.NET 1.1,不过这年头还有人用 ...

  2. sharepoint 2010 moss 培训教程 sharepoint开发经常需要用到的一些基本知识点

    列一下之前做的一些关于moss,sharepoint 2010的培训课程,主要是sharepoint开发经常需要用到的一些基本知识点. sharepoint应用篇 1.sharepoint 安装与部署 ...

  3. Arduino可穿戴开发入门教程Arduino开发环境介绍

    Arduino可穿戴开发入门教程Arduino开发环境介绍 Arduino开发环境介绍 Arduino不像我们使用的PC端操作系统一样,可以直接在操作系统中安装软件为操作系统编程.Arduino的软件 ...

  4. Java新手看招 常用开发工具介绍

    Java新手看招 常用开发工具介绍 Java的应用越来越广泛,学习Java的人也越来越多.学过程序设计的人知道,使用Basic进行程序设计,可以使用QBasic.Visual Basic等开发工具:使 ...

  5. esp32 python-ESP32及其开发板介绍

    ESP32及其开发板介绍¶ 有很多支持MicroPython的开发板,但我们教程的硬件选型,决定选择使用ESP32作为主控的开发板,原因如下: ESP32这款芯片是我国乐鑫公司设计和生产的物联网MCU ...

  6. java基础-Eclipse开发工具介绍

    java基础-Eclipse开发工具介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 所谓工欲善其事必先利其器,即将身为一名Java开发工程师怎么能没有一款好使的IDE呢?今天就 ...

  7. 将 SharePoint 开发与其他形式的开发进行比较

    从三个视点检查 SharePoint 开发很有用: 为 .NET Framework 构建可扩展的应用程序 构建数据库应用程序 构建传统的富客户端应用程序 将 SharePoint 应用程序与可扩展的 ...

  8. Visual Studio 2010 中的 SharePoint 开发

    Chai同学已经在他的blog上贴了文章,讲述VS2010中,针对SharePoint开发的一些增强.虽说VS2010还有一点点远,但是先了解一下也是不错的.嗯,在VS2008上,应该至少还会发布一个 ...

  9. 神经网络与深度学习——TensorFlow2.0实战(笔记)(二)(开发环境介绍)

    开发环境介绍 Python3 1.结构清晰,简单易学 2.丰富的标准库 3.强大的的第三方生态系统 4.开源.开放体系 5.高可扩展性:胶水语言 6.高可扩展性:胶水语言 7.解释型语言,实现复杂算法 ...

最新文章

  1. parentViewController
  2. Android中selector的使用
  3. Master/Slave知识
  4. 学习FreeRTOS的几点体会
  5. Perl用LWP实现GET/POST数据发送
  6. 【Python】 平方根
  7. 红橙Darren视频笔记 builder设计模式 navigationbar 导航栏第二版
  8. python 的__str__和__repr__有什么区别?
  9. 网络工程师职业规划(三)
  10. 导致存储过程重新编译的原因
  11. 短视频图像处理 OpenGL ES 实践
  12. C语言 · 进制转换
  13. cmd识别java命令却不识别javac
  14. 使用Java打印三角形
  15. ET99加密狗软件加密锁ET99开发工具下载
  16. 简历中的“项目经验”该怎么写?
  17. 介绍一个牛人的机器学习PPT
  18. 清明忆语 | 缅怀那些正渐行渐远的编程语言
  19. iOS之TabbarController和NavigationController框架
  20. 我是程序员,如假包换——如何用一句话证明你是程序员?

热门文章

  1. Python——assert(断言函数)
  2. Leetcode(11)-盛最多水的容器
  3. IIS7.0站点/虚拟目录中访问共享
  4. 开机显示c:\windows\windows32\config\system文件损坏或丢失的解决方法(收集)
  5. 268 missing number
  6. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol52]40一般来说SPA和DPA的区别是什么
  7. [Leedcode][JAVA][第289题][生命游戏]
  8. access是不是计算机编程,access编程简介之二:用宏还是VBA?
  9. c语言竖等于意思,C语言竖式问题
  10. 计算机网络实用期末试题和答案,计算机网络期末考试试题及答案(1)