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

概要

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

开发设计

注册事件.

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

    }

事件处理程序:

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

作者:spring yang

出处:http://www.cnblogs.com/springyangwc/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted @ 2011-07-25 19:30  spring yang  阅读(2759)  评论(1编辑  收藏  举报