SharePoint EventHandler 开发入门程序例子

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();
        }
    }
}

--------------------------

image

image

编译通过,生成 bin/Debug/CommandEventHandler.dll

3、打开vs08命令行工具

image 如果是在vista/win7打开,请使用“管理员身份打开”

image

C:/Windows/system32>gacutil /i D:/Projects/CommandEventHandler/CommandEventHandl
er/bin/Debug/CommandEventHandler.dll

注册到GAC

3、新建一个控制台程序工程

image 在Program.cs添加代码

代码:

----------------------

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获得

image

编译,执行这个exe文件,这几个方法便注册成功了。

4、现在来操作这个文档库

image

添加和删除

image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值