步步为营 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拖入到这个路径中即可。
代码很容易,想必大家一看就能明白,找到工程生成的可执行文件,运行:
这样就代表我们的Event Handler发布成功啦!至此,Event Handler的部署工作也就完成啦!
最后我们来测试下,进入文档库,删除那个“LINQ中文教程”word文件。到C:\下看下Log.txt:
作者:spring yang
出处:http://www.cnblogs.com/springyangwc/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架