Event Handler的开发与部署扩展练习:开发一个通用的EventHandler

 前面我们实现Event Handler的方法只适合文档列表的操作,实际上这也是SharePoint 2003能做到的事情,但2003中却不能为除文档之外的列表添加Event Handler。在2007中,会不会有所改进呢?比如用户进行删除一篇Announcements的动作,我们希望能提示没有权限删除,并阻止该次删除动作,该怎么做到呢?
   由于有了前面的经验,这里就简单的介绍一下:
  1、首先打开打开Microsoft Visual Studio ,创建一个名为Eallies.EventHandler.SP2007的Class Library。

 2、创建完成后,将默认的Class1.cs改名为ListHandler.cs。

 3、为项目添加Microsoft.SharePoint.dll的引用。
4、将项目的输出目录更改为C:\Inetpub\wwwroot\wss\VirtualDirectories\9001\_app_bin。
5、为项目创建强名称。
6、更改ListHandler.cs为如下的代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

using System.IO;

using System.Net.Mail;

 

namespace Eallies.EventHandler.SP2007

{

    public class ListHandler : SPItemEventReceiver

    {

        public override void ItemDeleting(SPItemEventProperties properties)

 

        {

 

           properties.Cancel = true;

 

           properties.ErrorMessage = "You have no access to delete it,shit"+DateTime.Now.ToString();

           SendMailLocalhost();

 

       }

        public static void SendMailLocalhost()

        {

 

            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();

            msg.To.Add("mail@msdev.local");

     

            msg.From = new MailAddress("mail@msdev.local", "johnny", System.Text.Encoding.UTF8);

            /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/

            msg.Subject = "这是测试邮件";//邮件标题            

            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码

            msg.Body = "邮件内容:测试event handler";//邮件内容

            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码

            msg.IsBodyHtml = false;//是否是HTML邮件

            msg.Priority = MailPriority.High;//邮件优先级

 

            SmtpClient client = new SmtpClient();

            //client.Host = "127.0.0.1";

            client.Host = msg.From.Host;

 

            object userState = msg;

            try

            {

                //client.SendAsync(msg, userState);

                client.Send(msg);

                //MessageBox.Show("发送成功");

            }

            catch (System.Net.Mail.SmtpException ex)

            {

                //MessageBox.Show(ex.Message, "发送邮件出错");

                throw ex;

            }

        }

 

    }

}

 

 7、向解决方案中添加一个名为Eallies.EventHandler.Register的Console Application项目。
  8、为Eallies.EventHandler.Register项目添加Microsoft.SharePoint.dll的引用。
  9、使用Reflector找到Eallies.EventHandler.SP2007项目的Assembly信息。

10、将Eallies.EventHandler.Register项目中的Program.cs更改为如下的代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

using System.Net.Mail;

 

namespace Eallies.EventHandler.Register

{

    class Program

    {

        static void Main(string[] args)

        {

            SPSite site = new SPSite("http://mossdev:7/Wodeweb");

 

            SPWeb web = site.OpenWeb("Wodeweb");

 

            SPList list = web.Lists["Announcements"];

            list.EventReceivers.Add(SPEventReceiverType.ItemDeleting, "Eallies.EventHandler.SP2007, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fc925c2936db9e66", "Eallies.EventHandler.SP2007.ListHandler");

 

        }

    }

}

 11、编译Eallies.EventHandler.SP2007项目,并将编译后的DLL加入到操作系统的GAC中。注意,这一步相当的重要,如果你是重新编译程序,也一定要重新加入这个DLL到GAC中,才能看到修改;

12、测试eventhandler

附文档:

https://files.cnblogs.com/sunjunlin/EventHandler%e5%bc%80%e5%8f%91%e4%b8%8e%e9%83%a8%e7%bd%b2.pdf

版权所有

作者:johnny 出处:http://www.cnblogs.com/sunjunlin 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/sunjunlin/p/1768058.html