好久没有写博客了。最近在使用SharePoint 2010中Timer Job的功能,有了一点心得,分享一下。

我个人认为SharePoint Timer Job和Windows Service或者是Schedule很相似,就是enable之后可以定时执行。开发的步骤如下:

1.   在VS中新建一个Class,这个Class继承Microsoft.SharePoint.Administration.SPJobDefinition,实现的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Mike.TimeJob
{
    public class MyFirstTimeJob:SPJobDefinition
    {
        public MyFirstTimeJob()
            :base()
        {}
        
        public MyFirstTimeJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            :base(jobName,service,server,targetType)
        {}
        
        public MyFirstTimeJob(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "Mike First Timer Job";
        }

public override void Execute(Guid targetInstanceId)
        {
            // get a reference to the current site collection's content database
            SPWebApplication webApplication = this.Parent as SPWebApplication;
            SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];

// get a reference to the "Tasks" list in the RootWeb of the first site collection in the content database
            SPList taskLlist=contentDb.Sites[0].RootWeb.Lists["Tasks"];

//create a new task, set the Title to the current day/time, and update the item
            SPListItem newTask = taskLlist.Items.Add();
            newTask["Title"] = DateTime.Now.ToString();
            newTask.Update();            
        }
    }
}
MyFirstTimeJob这个类最关键的就是需要override Execute方法,这里面可以写你自己想要实现的业务逻辑,我这里就是向Tasks List中每次新增一个时间信息,这个类需要注册到GAC中,是需要强命名的。

2. 在VS中新建第二个Class,这个Class是MyFirstTimeJob的安装类,在SharePoint Feature被激活的时候使用,这个Class继承了Microsoft.SharePoint.Administration.SPFeatureReceiver,实现的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Mike.TimeJob
{
    public class MyFirstTimeJobInstaller:SPFeatureReceiver
    {
        const string JOB_NAME = "MyFirstTimeJob";

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;
            // make sure the job isn't already registered
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == JOB_NAME)
                job.Delete();
            }
            // install the job
            MyFirstTimeJob myjob = new MyFirstTimeJob(JOB_NAME, site.WebApplication);
            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 5;
            myjob.Schedule = schedule;
            myjob.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 == JOB_NAME)
                    job.Delete();
            }
        }
    }
}

MyFirstTimeJobInstaller类中FeatureDeactivating方法用于在Feature被Disable时将已经存在的SPJobDefinition实例删除,FeatureActivated方法用于删除已经存在的SPJobDefinition实例,然后再新建实例,并设置Schedule,Schedule可以有SPYearlySchedule、SPMonthlySchedule、SPDailySchedule、SPHourlySchedule、SPMinuteSchedule等等,具体可以去查MSDN。

3. 将这2个Class注册到GAC中,因为是同一个Assembly,得到一个PublicKey。

安装GAC的命令

gacutil -i 目录\Mike.TimeJob.dll

4. 在创建Feature.XML文件

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
         Id="D4C9BB6B-E95D-4066-A0BA-EE5AAE79E3B3"
         Title="Mike First Timer Job"
         Description="Mike Hu's first timer job for adding now to tasks list"
         Scope="Site"
         Hidden="TRUE"
         Version="1.0.0.0"
         ReceiverAssembly="Mike.TimeJob, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d747f4e6e693450e"
         ReceiverClass="Mike.TimeJob.MyFirstTimeJobInstaller">
</Feature>

这里Feature中的PublicKeyToken填写的是第3步得到的Public Key.将这个feature.xml文件copy到C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\MyFirstTimeJob_Feature1中,注意MyFirstTimeJob_Feature1是自己创建的。

5. 安装Feature

stsadm -o installfeature -filename MyFirstTimeJob_Feature1\feature.xml -force

6. 重新启动ISS

iisreset

7. 激活Feature

stsadm -o activatefeature -filename MyFirstTimeJob_Feature1\feature.xml -url http://prsgi0001

其中http://prsgi0001表所起作用的Web Application,这是Timer Job已经安装成功,并设置好了Shedule。

8. stop SharePoint 2010 Timer
net stop sptimerv4

9. start SharePoint 2010 Timer
net start sptimerv4

这样在Central Administration--> Monitoring --> Timer Jobs --> Review Job Definition中找到安装好的Timer Job。

全部完成,并可以成功运行了。

10. 如果需Debug,需要attach to process到OWSTIMER.EXE这个process中。

参考:Andrew Connell, Creating Custom SharePoint Timer Jobs

创建SharePoint 2010 Timer Job相关推荐

  1. 在SharePoint 2010中创建网站的权限级别

    转:http://www.360sps.com/Item/CreatePermissionLevels.aspx 权限级别是SharePoint 2010新增加的功能,使我们对权限的设置又提高了一个层 ...

  2. SharePoint 2010管理中心创建网站集

    必须以管理员的身份登录SharePoint 2010管理中心主页,依次点击"应用程序管理"----"创建网站集" 鼠标放在"创建网站集"链接 ...

  3. SharePoint 2010 主题

    从SharePoint 2003开始,主题就作为一种自定义SharePoint外观的重要选项.SharePoint 2003和2007中的包括一系列开箱即用的主题,可以被网站所有者通过SharePoi ...

  4. SharePoint 2010在WIN7系统下建立开发环境

    SharePoint 2010在WIN7系统下建立开发环境 (MSDN上有比较详细的介绍,地址:http://msdn.microsoft.com/en-us/library/ee554869%28o ...

  5. 为SharePoint 2010创建Application Page

    如果不了解什么是Application Page,可以参考我以前写过的这篇文章.SharePoint 2010的页面模型没有太多的变化,基本和2007保持一致.对于开发人员而言,为SharePoint ...

  6. 创建与SharePoint 2010风格一致的下拉菜单 (续) 整合Feature Custom Action框架

    在上一篇"创建与SharePoint 2010风格一致的下拉菜单"文章中,演示了如何添加一个额外的菜单到页面顶端,并使其的样式风格与SharePoint 2010内置的菜单保持一致 ...

  7. 在SharePoint 2010中通过SQL数据源创建仪表板

    本文中,我们将讨论如何为仪表板创建数据源连接到SQL Server数据表. 注意:PerformancePoint Services是通过一个Windows身份验证委托的方式访问外部数据的.因此,外部 ...

  8. SharePoint 2010 使用代码创建视图查询条件

    在浏览器里使用SharePoint 2010新建视图时,可以设置查询条件,如下图: 本文要介绍的是如何使用代码更新已创建好的视图查询条件. 首先,在浏览器新建一个视图:Search,其他一切均保持默认 ...

  9. SharePoint 2010 创建联系人列表和使用联系人列表

    SharePoint 2010 创建联系人列表和使用联系人列表 作为一款协同办公系统,联系人列表是必不可少的,SharePoint 2010里提供了"联系人列表模板",我们可以基于 ...

最新文章

  1. 基于深度学习的低光照图像增强方法总结(2017-2019)| CSDN博文精选
  2. java 界面线性布局_怎么用java代码写一个线性布局;布局里面有两个按钮是水平的...
  3. vim 删除多行_Vim 可视化模式入门
  4. Oracle中Number类型字段使用.netTiers和CodeSmith问题的解决方案
  5. 7个设计模式的基本原则
  6. C语言实现数字串转数字
  7. Html + css 实现银行回执单 页面
  8. SuperSearch(超级网搜、超级网际搜索)V6.2 —— 让思考从搜索开始
  9. 【寒江雪】Unity中调用C++的函数和类
  10. Horizon Daas 模板转化映像ip地址检索获取失败
  11. goldendict 屏幕取词
  12. 计算机控制运行内存,运行内存
  13. python opencv选取特定颜色区域
  14. css实现精灵图片加载图片失败
  15. 什么是RAID?RAID有什么用?RAID原理是什么
  16. 网页设计与制作教程 项目1
  17. 微信域名批量检测的实现原理
  18. 区块链:游戏世界的中央银行
  19. css3动画实现奔跑的火柴人
  20. 机械制图计算机识图,二、机械制图与识读基础

热门文章

  1. 计算机网络学习-006
  2. mysql实践教学大纲_MySQL数据库基础实例教程(微课版)-教学大纲.doc
  3. 5GNR漫谈17:OFDM载波频偏估计(CFO)
  4. Windows dos命令使用总结(持续更新)
  5. kuka机器人报电源管理报错_江门市KUKA机器人电源维修中心
  6. Iterator使用
  7. 【slingshot】trajectory inference
  8. 那些年薪百万的程序员“咸鱼翻身”没有透露的秘密
  9. 机器人仿真平台——RobotHw
  10. 网易云音乐批量下载python