好久没有写博客了。

近期在使用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. 在 Visual Studio 2010 中创建 SharePoint 2010 事件接收器

    Microsoft Visual Studio 2010 提供了一个可用于生成事件接收器的项目类型,事件接收器会在 Microsoft SharePoint 2010 网站上选择事件之前或之后执行操作 ...

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

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

  3. SharePoint 2010在win7 x64 安装

    转:http://kaneboy.blog.51cto.com/1308893/328000 关于<SharePoint 2010应用程序开发指南>,我和杜伟同学正在撰写中,希望下半年早点 ...

  4. [原] SharePoint 2010 WebPart与Google地图系列 一:创建显示地图的WebPart

    摘要: 作为信息化先驱的产品SharePoint 2010竟然对GIS相关技术支持如此有限,试问现在哪个企业没有大量的项目需要结合Google地图来进行开发,单纯地从Google Javascript ...

  5. sharepoint 2010 使用自定义列表模版创建列表(2)

    前面用的方法是通过界面上操作,根据自定义模版,创建的列表.sharepoint 2010 使用自定义列表模版创建列表(1) 这里顺便记录多另一种方法,通过程序来创建. ---------------- ...

  6. 为SharePoint 2010中的FBA创建自定义登录页面

    SharePoint 2010中默认的FBA登录页面非常简单,只提供了一个Asp.Net的Login控件,让用户输入用户名和密码.在大多数情况下,我们需要定制这个页面以满足一些安全需求,比如为登录页面 ...

  7. SharePoint 2010 中创建超链接到Pop-Up对话框

    SharePoint 2010 中创建超链接到Pop-Up对话框         SharePoint 2010 推出了新式的带有阴影的弹出对话框,你感觉怎么样?我感觉倒是挺酷的.这样少打开了一个页面 ...

  8. 转载-SharePoint 2010 WebPart与Google地图系列 一:创建显示地图的WebPart

    [原] SharePoint 2010 WebPart与Google地图系列 一:创建显示地图的WebPart 摘要: 作为信息化先驱的产品SharePoint 2010竟然对GIS相关技术支持如此有 ...

  9. sharepoint 2010 创建自定义的ASP.NET Web Service (上)

    项目背景 根据客户需求在SharePoint 2010 中创建自定义的ASP.NET Web Service可以分为3种方式(我所知道的).废话少说,下面一一列举: 创建方式 MSDN 官方博客自己的 ...

随机推荐

  1. ch1 About thinking skills

    When confronted with a problem , we think about it. The issue, of course, is that our efforts may be ...

  2. CentOS 7下ElasticSearch集群搭建案例

    最近在网上看到很多ElasticSearch集群的搭建方法,本人在这人使用Elasticsearch5.0.1版本,介绍如何搭建ElasticSearch集群并安装head插件和其他插件安装方法. 一 ...

  3. div常用效果方法-transform

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  4. DatePickerDialog和TimePickerDialog(基于对话框显示时间和日期)

    public class MainActivity extends Activity implements android.view.View.OnClickListener{ private But ...

  5. HTML5——移动端的点击、拖拽

    移动端浏览器不支持mouse事件 https://www.cnblogs.com/joyco773/p/6519668.html https://www.cnblogs.com/yjhua/p/525 ...

  6. 系统信号-signal.h

    #define SIGSEGV 11 /* segmentation violation */ #define SIGSYS 12 /* bad argument to system call */ ...

  7. vue城市三级联动组件 vue-area-linkage

    Install the pkg with npm: // v5之前的版本 npm i --save vue-area-linkage // v5及之后的版本 npm i --save vue-area ...

  8. 梦想CAD控件网页版搜索图面上的文字

    在网页中查找到CAD控件图纸上的文字.点击此处在线演示. 主要用到函数说明: _DMxDrawX::NewSelectionSet 实例化一个构造选择集进行过滤,该类封装了选择集及其处理函数. _DM ...

  9. elk大纲

    一.ELK功能概览 1.检索 2.数据可视化--实时监控(实时刷新) nginx 访问量 ip地区分布图(大数据) 3.zabbix 微信联动报警 4.大数据日志分析平台(基于hadoop) 二.ka ...

  10. Xcode5编译ffmpeg

    命令行安装FFmpeg:git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg(或:到https://github.com/gabriel/ffmpeg ...