好久没有写博客了。

近期在使用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. MacOS 下安装 MySQL8.0 登陆 MySQL

    按照 官方教程 ,下载安装包,点击安装后,如需在命令行启动,还需设置命令路径: 在命令行中,打开配置文件 .bash_profile: vim ~/.bash_profile 在最后一行加上: PAT ...

  2. js实现左右点击图片层叠滚动特效

    需要加载js有 <script type="text/javascript" src="js/jquery-1.7.2.min.js"></s ...

  3. ElasticSearch学习笔记--一些规范,会持续更新

    我们在ElasticSearch中存储的数据一般是采用json的格式存储,所以ElasticSearch中有一个叫Mapper的东西用来定义jsonschema来规范这个json 但是这个mapper ...

  4. dom4j使用方法详解

    本文先做知识点的简单介绍,最后附完整案例. 一.解析XML文件 public class Foo { //url为XML文档地址 //自己封装了一个工具类 返回解析完成的document public ...

  5. ThinkPHP---案例2--部门管理功能

    [一]部门列表展示 分析: ①控制器DeptController.class.php ②方法showList(不要使用list方法,因为list是关键词) ③模板文件:showList.html 下面 ...

  6. 网络编程 - 简单的socket例子

    1.客户端 #客户端import socketclient=socket.socket() #生成socket连接对象client.connect(("localhost",696 ...

  7. 诊断:expdp导出时遇到错误ORA-31693和ORA-00922

    11.2.0.1使用数据泵expdp导出时,如果使用parallel,可能会遇到 ORA-: Table data object "OWNER"."TABLE" ...

  8. NOIP 2008 传纸条(洛谷P1006,动态规划递推,滚动数组)

    题目链接:P1006 传纸条 PS:伤心,又想不出来,看了大神的题解 AC代码: #include<bits/stdc++.h> #define ll long long using na ...

  9. enote笔记法的思考

    章节:enote笔记法的思考   why enote笔记法: key1)大脑喜欢颜色. 我们的大脑天生就喜欢颜色.对颜色很敏感,这是由我们人类过去的演化历程决定的. 你可以理解为,文字有了颜色,让这个 ...

  10. 字符串匹配「 KMP 算法 」

    引言 众所周知,字符串无论是在 OI 中还是别的计算机领域都占有比较大的比重,今天说的就是一个关于匹配字符串的算法——「 KMP 算法 」. 0x00 KMP 算法用于解决这样的一类问题:给定一个文本 ...