1、打开SharePoint3.0管理中心启用“向后兼容”
2、新建类库工程,命名为CommandEventHandler,在工程中新建类HandlerDo.cs
在工程中引入Microsoft.SharePoint.dll;
代码:
----HandlerDo.cs-----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.SharePoint;
namespace CommandEventHandler
{
public class HandlerDo : SPItemEventReceiver
{
public override void ItemDeleting(SPItemEventProperties properties)
{
string path = @"c:/delete_log.txt";
string content = "try to Delete item: " + DateTime.Now;
StreamWriter writer = new StreamWriter(path);
writer.WriteLine(content);
writer.Close();
//base.ItemDeleted(properties);
properties.Cancel = true;
properties.ErrorMessage = "Item deletion is no allowed by user:" + properties.UserDisplayName;
}
public override void ItemAdded(SPItemEventProperties properties)
{
//base.ItemDeleted(properties);
string path = @"c:/add_log.txt";
string content = "Add item: " + DateTime.Now;
StreamWriter writer = new StreamWriter(path);
writer.WriteLine(content);
writer.Close();
}
}
}
--------------------------
编译通过,生成 bin/Debug/CommandEventHandler.dll
3、打开vs08命令行工具
C:/Windows/system32>gacutil /i D:/Projects/CommandEventHandler/CommandEventHandl
er/bin/Debug/CommandEventHandler.dll
注册到GAC
3、新建一个控制台程序工程
代码:
----------------------
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;
namespace CustomEventHandleRegister
{
class Program
{
static void Main(string[] args)
{
SPSite site = new SPSite("http://apensu-pc:8000");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["edocuments"];
list.EventReceivers.Add(SPEventReceiverType.ItemAdded,
"CommandEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7129206d3231828f",
"CommandEventHandler.HandlerDo");
list.EventReceivers.Add(SPEventReceiverType.ItemDeleting,
"CommandEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7129206d3231828f",
"CommandEventHandler.HandlerDo");
}
}
}
----------------------
红色部分,关于CommandEventHandler.dll的信息,请使用Reflector.exe获得
编译,执行这个exe文件,这几个方法便注册成功了。
4、现在来操作这个文档库
添加和删除