时间:2020-09-03 21:47作者:admin来源:未知人气:300我要评论(0)
在sharepoint的开发和应用中,经常会使用到,需要定时执行或者更新数据,我们可以用sharepoint自带的timer job来实现。
1。创建一个sharepoint 项目,名称为TimerJobTest;
2。创建一个class文件,名称为TimerJobClass;继承SPJobDefinition,如下图
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
namespace TimerJobTest
{
public class TimerJobClass : SPJobDefinition
{
public TimerJobClass(): base(){}
public TimerJobClass(string jobName, SPService service, SPServer server,
SPJobLockType targetType)
: base(jobName, service, server, targetType)
{
}
public TimerJobClass(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = jobName;
}
public override void Execute(Guid contentDbId)
{
//这里就是我们要执行的函数方法
}
}
}
3。添加一个feature,名称为TimerJob,并且选择范围为site,如下图:
4。添加一个事件接收器,如下图:
5。需要override其中的两个函数,
override void FeatureActivated(SPFeatureReceiverProperties properties),部署timer job函数
override void FeatureDeactivating(SPFeatureReceiverProperties properties) 删除timer job函数
方法如下:
const string JOB_NAME = "TimerJobTest";
// Uncomment the method below to handle the event raised after a feature has been activated.
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
TimerJobClass Doc = new TimerJobClass(JOB_NAME, site.WebApplication);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 1;
Doc.Schedule = schedule;
Doc.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();
}
}
}
6。部署之后,到管理中心,作业定义中,查看是否已经部署成功,如下图,我们看到,timerjobTest已经成功部署,如下图
7。需要重新启动服务,如下图
这时候我们的timer job 就创建完成了。
相关阅读 SharePoint中Office文件无法打开的解决方案如何快速生成SharePoint测试大文件如何查询SharePoint Library中空文件夹?如何快速备份SharePoint Farm Solution如何解决Event Viewer中SharePoint Error - Event ID 8321SharePoint 2016 CU安装失败,"Exception: The upgraded database schema doesn't match the TargetSchema"的解决方案InfoPath Error “此文档库已经被重命名或删除,或者网络问题导致文件无法保存…” 的解决方案SharePoint 2013 App概述How to Shrink SharePoint Content Database Log File?Project Web App Feature无法开启的解决方案
热门文章 SharePoint 2016 图文安装教程 后面有激活序列号、密钥分享[SharePoint入门教程]一SharePoint发展、工具及术语如何用 SharePoint Online创建团队网站?SharePoint Iframe 报错“此内容不能显示在一个框架中”
最新文章
SharePoint中Office文件无法打开的解决方案如何快速生成SharePoint测试大文件
如何查询SharePoint Library中空文件夹?如何快速备份SharePoint Farm Solution如何解决Event Viewer中SharePoint Error - Event ID 8321SharePoint 2016 CU安装失败,"Exception: The upgraded database s
人气排行 SharePoint 2016 图文安装教程 后面有激活序列号、密钥分享[SharePoint入门教程]一SharePoint发展、工具及术语如何用 SharePoint Online创建团队网站?SharePoint Iframe 报错“此内容不能显示在一个框架中”SharePoint 2013 安装图解 SharePoint安装步骤图解教程SharePoint 如何开启访问请求[SharePoint入门教程]一SharePoint概述[SharePoint入门教程]一创建SharePoint母版页
盖楼回复X
(您的评论需要经过审核才能显示)
查看所有0条评论>>