概要
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拖入到这个路径中即可。
代码很容易,想必大家一看就能明白,找到工程生成的可执行文件,运行:
这样就代表我们的Event Handler发布成功啦!至此,Event Handler的部署工作也就完成啦!
最后我们来测试下,进入文档库,删除那个“LINQ中文教程”word文件。到C:\下看下Log.txt: