创建SharePoint 2010 Timer Job
好久没有写博客了。
近期在使用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中。
创建SharePoint 2010 Timer Job的更多相关文章
- 在 Visual Studio 2010 中创建 SharePoint 2010 事件接收器
Microsoft Visual Studio 2010 提供了一个可用于生成事件接收器的项目类型,事件接收器会在 Microsoft SharePoint 2010 网站上选择事件之前或之后执行操作 ...
- 在SharePoint 2010中创建网站的权限级别
转:http://www.360sps.com/Item/CreatePermissionLevels.aspx 权限级别是SharePoint 2010新增加的功能,使我们对权限的设置又提高了一个层 ...
- SharePoint 2010在win7 x64 安装
转:http://kaneboy.blog.51cto.com/1308893/328000 关于<SharePoint 2010应用程序开发指南>,我和杜伟同学正在撰写中,希望下半年早点 ...
- [原] SharePoint 2010 WebPart与Google地图系列 一:创建显示地图的WebPart
摘要: 作为信息化先驱的产品SharePoint 2010竟然对GIS相关技术支持如此有限,试问现在哪个企业没有大量的项目需要结合Google地图来进行开发,单纯地从Google Javascript ...
- sharepoint 2010 使用自定义列表模版创建列表(2)
前面用的方法是通过界面上操作,根据自定义模版,创建的列表.sharepoint 2010 使用自定义列表模版创建列表(1) 这里顺便记录多另一种方法,通过程序来创建. ---------------- ...
- 为SharePoint 2010中的FBA创建自定义登录页面
SharePoint 2010中默认的FBA登录页面非常简单,只提供了一个Asp.Net的Login控件,让用户输入用户名和密码.在大多数情况下,我们需要定制这个页面以满足一些安全需求,比如为登录页面 ...
- SharePoint 2010 中创建超链接到Pop-Up对话框
SharePoint 2010 中创建超链接到Pop-Up对话框 SharePoint 2010 推出了新式的带有阴影的弹出对话框,你感觉怎么样?我感觉倒是挺酷的.这样少打开了一个页面 ...
- 转载-SharePoint 2010 WebPart与Google地图系列 一:创建显示地图的WebPart
[原] SharePoint 2010 WebPart与Google地图系列 一:创建显示地图的WebPart 摘要: 作为信息化先驱的产品SharePoint 2010竟然对GIS相关技术支持如此有 ...
- sharepoint 2010 创建自定义的ASP.NET Web Service (上)
项目背景 根据客户需求在SharePoint 2010 中创建自定义的ASP.NET Web Service可以分为3种方式(我所知道的).废话少说,下面一一列举: 创建方式 MSDN 官方博客自己的 ...
随机推荐
- Selenium学习第二天,了解Selenium工作模式与学习Selenium需要具备的知识与工具。
Selenium学习网站: 1.http://www.ltesting.net/ceshi/open/kygncsgj/selenium/2014/0408/207237.html——好像是对API的 ...
- 数据库——DBUtils和连接池
第一章 DBUtils如果只使用JDBC进行开发,我们会发现冗余代码过多,为了简化JDBC开发,本案例我们讲采用apache commons组件一个成员:DBUtils.DBUtils就是JDBC的简 ...
- 数据导出为Excel(未完)
更多详细内容 view页面: function Download() { //多个查询条件 dateStart = $("#j_dataTimeStart").datebox(&q ...
- <东方梦符祭> N2无尽40波通关
先上图吧 阵容:纯粹 + 伪魔法队 主C:神妈 露米娅(我觉得不厉害了) 灵梦 控制:琪露诺 + 蕾蒂 永江依玖(听说很厉害 没培育满 没看到效果) 挂件:铃仙挂机 帕秋莉 大妖精(链神妈) 圣今天才 ...
- 如何使用fio模拟线上环境
线上表现 这里我想通过fio来模拟线上的IO场景,那么如何模拟呢? 首先使用iostat看线上某个盘的 使用情况,这里我们需要关注的是 avgrq-sz, avgrq-qz. #iostat -dx ...
- 完善本地搭建的jekyll环境(Windows)
序:上篇文章虽然在本地搭建好了jekyll环境,但是却存在一些问题,如通过jekyll new创建的站点无法正常跑起来.中文编码有问题.这说明之前搭建的环境有不周之处. PS:因之前自己搭建环境时并未 ...
- LeetCode_18 4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- 通过HTTP的HEADER完成各种骚操作
作为一名专业的切图工程师,我从来不care网页的header,最多关心Status Code是不是200.但是HEADER真的很重要啊,客户端从服务器端获取内容,首先就是通过HEADER进行各种沟通! ...
- Flask保存或解压上传的文件
import os import uuid import shutil import zipfile from flask import Flask, render_template, request ...
- Java生成PDF文件(转)
原文地址:https://www.cnblogs.com/shuilangyizu/p/5760928.html 一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iT ...