好久没有写博客了。

近期在使用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. 使用SpringMvc的一个注意事项

    在Intelij Idea下,如果在新建项目时使用了自带的模板,那么自动生成的web.xml里的DispatcherServlet配置节点默认的servlet-mapping是这样的: 而习惯上,我们 ...

  2. jboss项目设置域名

    1.在jboss-web.xml中添加<virtual-host>www.ceshi.com</virtual-host> <jboss-web> <cont ...

  3. oracle dos命令

    1.无账户密码登录数据库:sqlplus/nolog 后面不能加分号,否则不能识别 2.登录数据库:sqlplus 3.在sql下测试连接性:conn oracle_name/oracle_passw ...

  4. Microsoft SQL Server学习(七)--函数视图

    系统函数 视图 索引 1.系统函数 (1) ()数学函数 Abs() 绝对值 Floor() 向下取整 Ceiling() 向上取整 Sin() 返回指定角度(以弧度为单位)的三角正弦值 Pi() 圆 ...

  5. Linux基础之操作系统

    一.什么是操作系统 简单来说,操作系统就是一个协调.管理和控制计算机硬件资源和软件资源的控制程序. 二.操作系统存在的意义 究根结底,我们日常对计算机的管理是对计算机硬件的管理.经过近百年的时间,现代 ...

  6. (转) 淘淘商城系列——Redis集群的搭建

    http://blog.csdn.net/yerenyuan_pku/article/details/72860432 本文我将带领大家如何搭建Redis集群.首先说一下,为何要搭建Redis集群.R ...

  7. ThinkPHP---辅助方法

    [三]Tp常见的辅助方法 原生SQL语句里除了目前所使用的基本操作增删改查,还有类似于group.where.order.limit等这样的字句. ThinkPHP封装了相应的子句方法:封装的方法都在 ...

  8. oracle关闭

    Alert log 要每天查看 abort 关闭冷备会无法使用

  9. XGBoost参数中文翻译以及参数调优

    XGBoost:参数解释:https://blog.csdn.net/zc02051126/article/details/46711047 机器学习系列(11)_Python中Gradient Bo ...

  10. 总结struts2 iterator status的用法

    1:#status.odd 是否奇数行 2:#status.count 当前行数 3:#status.index 当前行的序号,从0开始『#status.count=#status.index+1』 ...