步步为营 SharePoint 开发学习笔记系列 八、SharePoint EventHandler开发

概要

     SharePoint的EventHandler主要有Web Level,List Level,List Item Level,Email几种。SharePoint的event handler主要是继承SPWebEventReceiver, SPEmailEventReceiver, SPListEventReceiver和SPItemEventReceiver类去实现其中相应的方法来完成我们的需求。

开发设计

注册事件.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/// <summary>
/// EventHandler
/// </summary>
class EventHandler
{
    private string _assemblyFullName = string .Empty;
    public string AssemblyFullName
    {
        get
        {
            return _assemblyFullName;
        }
    }
    private string _className = string .Empty;
    public string ClassName
    {
        get
        {
            return _className;
        }
    }
    private SPEventReceiverType _eventType;
    private SPEventReceiverType EventType
    {
        get
        {
            return _eventType;
        }
    }
    private string _data = string .Empty;
    public string Data
    {
        get
        {
            return _data;
        }
    }
    private bool _deleteExistingEvents = false ;
    public bool DeleteExistingEvents
    {
        get
        {
            return _deleteExistingEvents;
        }
    }
    /// <summary>
    /// EventHandler
    /// </summary>
    /// <param name="handlerAssembly"></param>
    /// <param name="handlerClass"></param>
    /// <param name="eventType"></param>
    /// <param name="data"></param>
    /// <param name="deleteExisting"></param>
    public EventHandler( string handlerAssembly, string handlerClass, SPEventReceiverType eventType, string data, bool deleteExisting)
    {
        this ._eventType = eventType;
        this ._data = data;
        this ._assemblyFullName = handlerAssembly;
        this ._className = handlerClass;
        this ._deleteExistingEvents = deleteExisting;
    }
    /// <summary>
    /// Registers the event handler on the list with the specified name in the specified web.
    /// The function will not throw errors, but will return a string with any error or success description (a log)
    /// </summary>
    /// <param name="web">The SPWeb object of the sharepoint web site containing the list to attach the event handler to</param>
    /// <param name="listName">The name of the list to attach the event to</param>       
    /// <returns>A log of the event assignment</returns>
    public string RegisterEvent(SPWeb web, SPList list)
    {
        StringBuilder sb = new StringBuilder( string .Empty);
        try
        {
            SPEventReceiverDefinition eventReceiver = list.EventReceivers.Add();
            eventReceiver.Name = list.Title + EventType;
            eventReceiver.Type = this .EventType;
            eventReceiver.Assembly = this .AssemblyFullName;
            eventReceiver.Class = this .ClassName;
            eventReceiver.Data = this .Data;
            eventReceiver.Update();
            list.Update();
        }
        catch (Exception ex1)
        {
            sb.AppendLine( "Could not register the event on the list in this site:" + Environment.NewLine + ex1.Message);
        }
        return sb.ToString();
    }
}

事件处理程序:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
namespace SharePointBlog
{
    public class TestEventHandle : SPItemEventReceiver
    {
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            try
            {
                SaveLog();
            }
            catch (Exception ex)
            {
                properties.ErrorMessage = ex.Message;
                properties.Cancel = true ;
            }
        }
        public void SaveLog()
        {
            string path = @"c:\Log.txt" ;
            string text = "删除Item" + ":" + DateTime.Now.ToString();
            StreamWriter writer = new StreamWriter(path);
            writer.Write(text);
            writer.Close();
        }
    }
}

向SharePoint中部署Event Handler

和WebPart有所不同的是,Event Handler的dll需要放到GAC(Global Assembly Cache)中,而不能放在SharePoint网站的bin文件夹中,所以我们生成的dll必须进行强签名,这也就是上面为什么添加密钥文件的目的。

GAC的系统路径为:C:\WINDOWS\assembly,直接将生成的Event Handler dll拖入到这个路径中即可。

1

代码很容易,想必大家一看就能明白,找到工程生成的可执行文件,运行:

2

这样就代表我们的Event Handler发布成功啦!至此,Event Handler的部署工作也就完成啦!

最后我们来测试下,进入文档库,删除那个“LINQ中文教程”word文件。到C:\下看下Log.txt:

3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值