• 为列表类型绑定Event Receiver


    为列表类型绑定Event Receiver可以通过添加一个feature, 并在feature中添加一个receiver element来完成.

    具体详见SharePoint中的EventReceiver 之二 绑定Event Receiver到列表类型或列表实例.

    要创建一个Event Receiver, 你必须创建继承自一个特别的由WSS Object Model的event receiver的基类. 还一个很重要的地方需要注意的是, event receiver类必须在投入使用或测试之前, 编译到强命名的程序集, 并被部署到GAC中.

    下面的例子中的类继承自SPListEventReceiver, 所以它可以处理基于列表的事件. 就像所有的event receiver类中一样, 你通过重写在基类里定义的方法来创建event handler方法的实现.

    using System;
    using Microsoft.SharePoint;
    
    namespace LitwareTypes
    {
        public class VendorListEventReceiver : SPListEventReceiver
        {
    
            public override void FieldAdding(SPListEventProperties properties)
            {
                properties.ErrorMessage = "You cannot change this list schema!";
                properties.Cancel = true;
            }
    
            public override void FieldUpdating(SPListEventProperties properties)
            {
                properties.ErrorMessage = "You cannot change this list schema!";
                properties.Cancel = true;
            }
    
            public override void FieldDeleting(SPListEventProperties properties)
            {
                properties.ErrorMessage = "You cannot change this list schema!";
                properties.Cancel = true;
            }
        }
    }
    

    这个VendorListEventReceiver通过重载名字为FieldAdding, FieldUpdating, 和FieldDeleting的三个方法提供了三个event handler. 因为所有这三个events都是在事件发生之前被处理的, 不管是那种原因引起事件的触发, 这些event handlers都可以取消掉事件. 在这种情况下, 三个event handler使用SPListEventProperties参数来赋予一个错误信息, 然后取消了事件.  在这些event handler被恰当地绑定到列表之后, 他们会阻止用户(即使是有管理员权限的用户)向列表中添加, 修改, 或删除任何一个field.

    现在让我们来讨论一下如何把event receiver类中的event handler与一个列表类型绑定起来. 你可以通过在一个feature中添加一个receiver element来完成绑定. Receivers Element有一个值为10001的ListTemplateId 属性, 它绑定receivers到自定义的列表类型的所有的实例上. 注意, 在Receiver element之中, 每一个event handler都有一个独立的Receivers element. 你应该观察到每一个event handler方法都必须绑定到它自己的Receiver Element上, 因为没有其他的方法能够一次绑定多个event handler.

    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <!-- Receivers element can only be used in feature where Scope=Web -->
      <Receivers ListTemplateId="10001" >
        <Receiver>
          <Name>Field Adding Event</Name>
          <Type>FieldAdding</Type>
          <Assembly>LitwareTypes, [full 4-part assembly name goes here] </Assembly>
          <Class>LitwareTypes.VendorListEventReceiver</Class>
          <SequenceNumber>1000</SequenceNumber>
        </Receiver>
        <Receiver>
          <Name>Field Updating Event</Name>
          <Type>FieldUpdating</Type>
          <Assembly>LitwareTypes, [full 4-part assembly name goes here] </Assembly>
          <Class>LitwareTypes.VendorListEventReceiver</Class>
          <SequenceNumber>1000</SequenceNumber>
        </Receiver>
        <Receiver>
          <Name>Field Deleting Event</Name>
          <Type>FieldDeleting</Type>
          <Assembly>LitwareTypes, [full 4-part assembly name goes here] </Assembly>
          <Class>LitwareTypes.VendorListEventReceiver</Class>
          <SequenceNumber>1000</SequenceNumber>
        </Receiver>
      </Receivers>
    </Elements>

    上面的通过Receivers element来绑定event handler的技术有几个值得注意的局限性.

    • 首先, Receivers element只能被用在站点范围的features中. 它不能被使用在scope为其他等级的feature中, 这意味着我们不能使用这种绑定event的技术站点集级别的feature中.
    • 第二, Receiver element仅允许你绑定event handler到一个列表类型上. 它不提供给你绑定event handlers到列表实例或者绑定到一个content type的灵活性. 在许多情况下, 你会需要WSS object model来绑定你的event handler, 因为这种通过Object Model的方式提供了更大的灵活性.

    现在我们来讨论一下如何绑定event handlers到一个列表实例上, 而不是绑定到列表类型上. 下面的例子展现了使用WSS Object Model来绑定VendorListReceiver类的event handler到名为Vendors的列表实例上. 这里的代码是写在FeatureActived方法中的. 当你使用这种方式添加event handler的时候, 事件绑定的信息会被写入内容数据库中, 就如同其他修改目标列表的自定义一样.

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPSite siteCollection = (SPSite)properties.Feature.Parent;
        SPWeb site = siteCollection.RootWeb;
        SPList lstVendors = site.Lists["Vendors"];
        string asmName = "LitwareTypes, [full 4-part assembly name goes here] ";
        string listReceiverName = "LitwareTypes.VendorListEventReceiver";
        // add event receiver to fire before new column is added
        lstVendors.EventReceivers.Add(SPEventReceiverType.FieldAdding,
                                      asmName, listReceiverName);
        // add event receiver to fire before existing column is updated
        lstVendors.EventReceivers.Add(SPEventReceiverType.FieldUpdating,
                                      asmName, listReceiverName);
        // add event receiver to fire before existing column is updated
        lstVendors.EventReceivers.Add(SPEventReceiverType.FieldDeleting,
                                      asmName, listReceiverName);
    }

    除了可以为列表类型添加event receiver以外, MOSS 2007还可以在如下的三种对象的实例上添加Event Receiver.

    • SPList 
    • SPContentType 
    • SPWeb

    为他们添加event receiver可以通过SharePoint Object Model来完成, 核心代码为以下的函数调用.

    SPEventReceiverDefinitionCollection.Add 

    有一个免费的为sharepoint中的列表, contenttype, 站点添加event receiver的工具, 非常好用. 点击这里下载可执行文件和源代码. 

    添加的关键代码片段如下:

    if (node.Tag is SPList)
    {
        SPList list = (SPList)node.Tag;
        list.EventReceivers.Add
            ((SPEventReceiverType)Enum.Parse(typeof(SPEventReceiverType), comboBoxEventType.Text),
             textBoxAssemlby.Text, comboBoxClasses.Text);
        list.Update();
        flag = true;
    }
    if (node.Tag is SPContentType)
    {
        SPContentType ct = (SPContentType)node.Tag;
        ct.EventReceivers.Add
            ((SPEventReceiverType)Enum.Parse(typeof(SPEventReceiverType), comboBoxEventType.Text),
             textBoxAssemlby.Text, comboBoxClasses.Text);
        ct.Update();
        flag = true;
    }
    if (node.Tag is SPWeb)
    {
        SPWeb w = (SPWeb)node.Tag;
        w.EventReceivers.Add
            ((SPEventReceiverType)Enum.Parse(typeof(SPEventReceiverType), comboBoxEventType.Text),
             textBoxAssemlby.Text, comboBoxClasses.Text);
        w.Update();
        flag = true;
    }

    移出的关键代码如下:

    TreeNode node = treeViewItems.SelectedNode;
    SPEventReceiverDefinition er = (SPEventReceiverDefinition)node.Tag;
    if (MessageBox.Show("Are you sure you want to remove this event handler?", "Event Handler Explorer", 
    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
    {
        er.Delete();

    下面的连接有介绍使用方法.

    Free Tool: Register Event Handlers Programmatically (WSS 2007)

    http://www.u2u.info/Blogs/Patrick/Lists/Posts/Post.aspx?ID=1547

  • 相关阅读:
    背水一战 Windows 10 (61)
    背水一战 Windows 10 (60)
    背水一战 Windows 10 (59)
    背水一战 Windows 10 (58)
    背水一战 Windows 10 (57)
    背水一战 Windows 10 (56)
    背水一战 Windows 10 (55)
    背水一战 Windows 10 (54)
    背水一战 Windows 10 (53)
    背水一战 Windows 10 (52)
  • 原文地址:https://www.cnblogs.com/jhabb/p/2312249.html
Copyright © 2020-2023  润新知