[原]SharePoint列表与文档库EventHandeler
列表的EventHandeler:
1.继承SPItemEventReceiver
2.将SPItemEventReceiver中的方法重写就可以了,但是删除的时候要记得是重写Deleting方法
3.部署将dll拷贝到GAC中
Code
文档库的EventHandler:
实现步骤:
(1)类要继承 IListEventSink。
(2)重载事件
void IListEventSink.OnEvent(Microsoft.SharePoint.SPListEvent listEvent) {
if (listEvent.Type == SPListEventType.Insert)//增加{}
(1)类要继承 IListEventSink。
(2)重载事件
void IListEventSink.OnEvent(Microsoft.SharePoint.SPListEvent listEvent) {
if (listEvent.Type == SPListEventType.Insert)//增加{}
if (listEvent.Type == SPListEventType.Delete)//删除{}
if (listEvent.Type == SPListEventType.Update )//修改{}
}
(3) 添加强命名
if (listEvent.Type == SPListEventType.Update )//修改{}
}
(3) 添加强命名
部署步骤:
(1)把EventHandler的DLL放在服务器里面的GAC里面.
(2)然后在文档库设置的高级设置里,把DLL的信息填入
(如果没有,请在管理中心->应用程序管理->Web 应用程序常规设置-> 向后兼容的事件处理程序->启用).
(3)重启IIS.
部署程序:
Eventhandler-Deploy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace Deploy
{
class Program
{
static void Main(string[] args)
{
try
{
using(SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using(SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
site.AllowUnsafeUpdates = true;
SPList list = web.Lists["用户信息"];
string assName = "xxx.xxx.xxx.xxx, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ba17c498abfc3b38";
string className = "xxx.xxx.xxx.xxx";
list.EventReceivers.Add(SPEventReceiverType.ItemUpdating,assName,className);
list.EventReceivers.Add(SPEventReceiverType.ItemDeleting,assName,className);
Console.WriteLine("部署已完成");
Console.ReadLine();
}
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace Deploy
{
class Program
{
static void Main(string[] args)
{
try
{
using(SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using(SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
site.AllowUnsafeUpdates = true;
SPList list = web.Lists["用户信息"];
string assName = "xxx.xxx.xxx.xxx, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ba17c498abfc3b38";
string className = "xxx.xxx.xxx.xxx";
list.EventReceivers.Add(SPEventReceiverType.ItemUpdating,assName,className);
list.EventReceivers.Add(SPEventReceiverType.ItemDeleting,assName,className);
Console.WriteLine("部署已完成");
Console.ReadLine();
}
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}