使用SharePoint计时器作业,您可以每天/每小时/分钟创建和执行流程。
部署后,计时器作业定义将执行您的自定义代码。
部署计时器作业的最简单(也是更常见)的方法是按功能。
激活功能后,“ FeatureActivated”方法上的自定义代码应安装计时器作业。
这是如何创建和部署计时器作业定义的完整示例。
- 在“ Visual Studio”中,创建新的“ WSPBuilder”项目。
- 现在,我们将添加将安装计时器作业定义的功能。
- 右键单击项目->'添加'->'新项目'。
- 在“ WSPBuilder”下,选择“具有接收器的功能”。
- 这将使用一个实现功能部件安装/激活方法的类来创建一个新功能部件。
- 出现对话框时,键入您的计时器作业名称和描述。
- 最后,在“范围”部分中选择“ WebApplication”。
- *注意:您还可以选择其他几个范围。
- “农场”:该功能将安装在整个服务器场中。
- “站点”:该功能将安装在单个网站集(以及所有子站点)上。
- “ Web”:该功能将安装在特定的网站(子网站)上。
- feature.xml-包含功能的定义。
- elements.xml
- TimerJobInstaller.cs-包含安装/激活事件的替代方法。
现在,我们将创建计时器作业本身。
右键单击“ FeatureCode”文件夹,“添加”->“类”。
键入您的计时器作业的名称(我用一个唯一的名称来调用它:“ MyTimerJob.cs”)。
这将添加一个简单的类。 为了使此类可用作计时器作业,它应继承“ SPJobDefinition”类。
检查“ MyTimerJob.cs”类的完整代码:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.IO;
namespace WSPBuilderProject1.FeatureCode
{
public class MyTimerJob : SPJobDefinition
{
public MyTimerJob() : base()
{
this.Title = "My Timer Job";
}
public MyTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base(jobName, service, server, targetType)
{
this.Title = "My Timer Job";
}
public MyTimerJob(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = "My Timer Job";
}
public override void Execute(Guid contentDbId)
{
//TODO: write here the code of your job!
}
}
}
该类覆盖了将执行作业的自定义代码的“ Execute”方法!
使用这种方法,您可以编写所需的任何内容。
例如:从列表中删除项目,创建站点,从文档库复制文档等的代码。
现在我们的计时器作业已准备就绪,我们应该返回功能接收器类('TimerJobInstaller.cs'),并在此处编写安装作业的代码。
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using WSPBuilderProject1.FeatureCode;
namespace WSPBuilderProject1
{
class TimerJobInstaller : SPFeatureReceiver
{
const string SYNC_JOB_NAME = "My_Timer_Job";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
// make sure the job isn't already registered
foreach (SPJobDefinition job in webApp.JobDefinitions)
{
if (job.Name == SYNC_JOB_NAME)
job.Delete();
}
// install the job
MyTimerJob myJob = new MyTimerJob(SYNC_JOB_NAME, webApp);
// Set the schedule - nightly at 23:00 pm
//SPSchedule customSchedule = SPSchedule.FromString("daily at 23:00");
//syncJob.Schedule = customSchedule;
//Set the schedule - every minute
//SPMinuteSchedule minuteSchedule = new SPMinuteSchedule();
//minuteSchedule.BeginSecond = 0;
//minuteSchedule.EndSecond = 59;
//minuteSchedule.Interval = 3;
//syncJob.Schedule = minuteSchedule;
//Set the schedule - hourly between 0 and 59
SPSchedule schedule = SPSchedule.FromString("hourly between 0 and 59");
myJob.Schedule = schedule;
myJob.Update();
}
catch (Exception e)
{
}
}
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 == SYNC_JOB_NAME)
job.Delete();
}
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
}
}
}
此代码将安装计时器作业,并将其计划设置为每小时(每小时)。
我在注释中添加了更多计划计时器作业的方法。
请注意,我也重写了“ FeatureDeactivating”方法-当停用该功能时,它将删除作业。
而已! 我们的计时器作业已准备好进行部署。
要部署计时器作业,我们需要安装并激活该功能。 然后-该功能将完成其余工作(通过我们在“ FeatureActivated”方法中编写的代码安装计时器作业)。
- 首先,我们需要将DLL复制到GAC。
- 建立您的专案。
- 右键单击项目->'WSPBuilder'->'复制到GAC'。
- 然后,我们应该将该功能复制到12个配置单元中的FEATURES目录中。
- 右键单击项目->'WSPBuilder'->'复制到12个配置单元'。
- 现在,我们应该安装并激活该功能。
- 打开cmd.exe,然后键入
cd "Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN"
。 然后键入以下两个命令:- stsadm -o installfeature-文件名TimerJobInstaller \ feature.xml -force
- stsadm -o activatefeature-文件名TimerJobInstaller \ feature.xml -url http:// il-moss-dev5:6680 / sites / site1
现在,您可以在“中央管理”中看到新安装的计时器作业。
管理中心>操作>计时器作业定义。
From: https://bytes.com/topic/asp-net/insights/902681-how-create-deploy-sharepoint-2007-timer-job