步步为营 SharePoint 开发学习笔记系列 八、SharePoint EventHandler开发
概要
SharePoint的EventHandler主要有Web Level,List Level,List Item Level,Email几种。SharePoint的event handler主要是继承SPWebEventReceiver, SPEmailEventReceiver, SPListEventReceiver和SPItemEventReceiver类去实现其中相应的方法来完成我们的需求。
开发设计
注册事件.
view sourceprint?01 /// <summary>
02 /// EventHandler
03 /// </summary>
04 class EventHandler
05 {
06 private string _assemblyFullName = string.Empty;
07 public string AssemblyFullName
08 {
09 get
10 {
11 return _assemblyFullName;
12 }
13 }
14 private string _className = string.Empty;
15 public string ClassName
16 {
17 get
18 {
19 return _className;
20 }
21 }
22 private SPEventReceiverType _eventType;
23 private SPEventReceiverType EventType
24 {
25 get
26 {
27 return _eventType;
28 }
29 }
30 private string _data = string.Empty;
31 public string Data
32 {
33 get
34 {
35 return _data;
36 }
37 }
38 private bool _deleteExistingEvents = false;
39 public bool DeleteExistingEvents
40 {
41 get
42 {
43 return _deleteExistingEvents;
44 }
45 }
46
47 /// <summary>
48 /// EventHandler
49 /// </summary>
50 /// <param name="handlerAssembly"></param>
51 /// <param name="handlerClass"></param>
52 /// <param name="eventType"></param>
53 /// <param name="data"></param>
54 /// <param name="deleteExisting"></param>
55 public EventHandler(string handlerAssembly, string handlerClass, SPEventReceiverType eventType, string data, bool deleteExisting)
56 {
57 this._eventType = eventType;
58 this._data = data;
59 this._assemblyFullName = handlerAssembly;
60 this._className = handlerClass;
61 this._deleteExistingEvents = deleteExisting;
62 }
63 /// <summary>
64 /// Registers the event handler on the list with the specified name in the specified web.
65 /// The function will not throw errors, but will return a string with any error or success description (a log)
66 /// </summary>
67 /// <param name="web">The SPWeb object of the sharepoint web site containing the list to attach the event handler to</param>
68 /// <param name="listName">The name of the list to attach the event to</param>
69 /// <returns>A log of the event assignment</returns>
70 public string RegisterEvent(SPWeb web, SPList list)
71 {
72 StringBuilder sb = new StringBuilder(string.Empty);
73 try
74 {
75 SPEventReceiverDefinition eventReceiver = list.EventReceivers.Add();
76 eventReceiver.Name = list.Title + EventType;
77 eventReceiver.Type = this.EventType;
78 eventReceiver.Assembly = this.AssemblyFullName;
79
补充:Web开发 , ASP.Net ,