好久没有写博客了。

近期在使用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

最新文章

  1. AngularJs之六(服务)
  2. 思维导图MindManager的文件格式与例图
  3. Object.create() 和 __proto__ 的关系
  4. MVC 路由模块分析(一)
  5. SpringMVC注册拦截器
  6. java中的集合链表
  7. git逻辑和基本命令
  8. Spring Factory
  9. 知识图谱实战开发案例剖析-番外篇(1)- Neo4j是否支持按照边权重加粗和大数量展示
  10. Map字符串类型去掉空格处理
  11. Struts 2 初步入门(二)
  12. BOM对象思维导图
  13. 20165203 实验二 Java面向对象程序设计
  14. 利用struts2&lt;s:token&gt;标签防止用户重复提交
  15. (转) 使用vivado创建工程 1
  16. python练习题3--for
  17. hdu6325 Interstellar Travel 凸包变形
  18. ReactNative-JS 调用原生方法实例代码(转载)
  19. MySQL巧用FIND_IN_SET和GROUP_CONCAT函数减少Java代码量
  20. Dwarves, Hats and Extrasensory Abilities Codeforces - 1063C

热门文章

  1. 理解 CSS 布局和块级格式上下文
  2. Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1
  3. HTML——meta
  4. HDU_1143_tri tiling
  5. sharepoint services
  6. 通过Oracle函数SQL实现C# String.Format字符串格式化功能
  7. 面向对象程序设计--Java语言第三周编程题:查找里程
  8. jekyll本地环境搭建(Windows)
  9. TWaver 3D作品Viewer查看器
  10. * screen recording on Ubuntu