与feature自身相关的event有四种安装 删除 激活 取消。我们就是利用activate的时候,来控制对job的设置,利用deactivate的时候,将指定的job删除。
1.在VS中创建一个class library的工程。在reference中加入Microsoft.sharepoint.dll,这个dll可以在 C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI下找到。
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace TimerJobDemo
{
class TimerJobDemoFeature:SPFeatureReceiver
{
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
}
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
SPSite site = new SPSite("http://litwaredemo");
SPWeb rootweb = site.RootWeb;
SPList list = rootweb.Lists["ForTimerJob"];
SPListItem item = list.Items.Add();
item["Title"] = "This is a test";
item.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace TimerJobDemo
{
class TimerJobDemoFeature:SPFeatureReceiver
{
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
}
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
SPSite site = new SPSite("http://litwaredemo");
SPWeb rootweb = site.RootWeb;
SPList list = rootweb.Lists["ForTimerJob"];
SPListItem item = list.Items.Add();
item["Title"] = "This is a test";
item.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
}
}
}
以上就是feature event handler的代码,我们可以看到,在featurereceiver中我们需要重载四个不同时期的函数,但是我们只是对其中的featureactivate进行了完整的实现。
2.在同一个工程中新建一个文件夹,命名为TimerJobDemo,创建一个Feature.xml(具体的过程可以参看前面的两篇)
这个feature的任务就是创建,并注册一个新的timer job,所以这个feature十分的简单。相比前面提到的feature,它多了两个十分主要的属性。
<
Feature
xmlns
="http://schemas.microsoft.com/sharepoint/"
Title
="TimerJobDemo"
Id
="2D301FC8-DB43-4d5e-93DE-8F722054F4EE"
ReceiverAssembly
="TimerJobDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=80614946b1f438b6"
ReceiverClass
="TimerJobDemo.TimerJobDemoFeature"
Scope
="WebApplication"
/>
这个就是这个feature的全部内容,只有一个节点。Titile ID等属性我们前面都已经提到了,这里出现两个新的属性,就是ReceiverAssembly 和ReceiverClass。ReceiverAssembly 指定的是FeatureReceiver所在的程序集,而ReceiverClass指定的是FeatureReceiver所在的类。在激活feature前,我们需要将已经强签名的程序集Gac。然后安装和激活feature。激活后,我们到前面代码中提到的list中,看看是不是多了一个新的item。