SP2010以编程方式将事件接收器附加到特定列表 - 针对所有列表运行

时间:2012-09-03 12:40:58

标签: sharepoint-2010 eventreceiver

我将事件接收器附加到单个列表(Web范围)。但ER运行Web中的所有列表。这个question表示部署ER的功能必须是Web范围。情况就是这样。

功能以编程方式激活,绑定到TLS中列表的ER。

newProjectWeb.Features.Add(new Guid("57e21870-6285-4e0a-b9a0-067f774492ae"));

请参阅下面的代码。我错过了更新或其他任何内容吗?

提前感谢您的帮助。

public void AddEventReceiverToMemberList()
    {
        try
        {
            _clsLists.AddEventReceiverToList(Web, ProjectMemberList.LIST_INTERNAL_NAME, typeof(SCMUProjectMemberList), SPEventReceiverType.ItemAdded);
            _clsLists.AddEventReceiverToList(Web, ProjectMemberList.LIST_INTERNAL_NAME, typeof(SCMUProjectMemberList), SPEventReceiverType.ItemDeleting);
            _clsLists.AddEventReceiverToList(Web, ProjectMemberList.LIST_INTERNAL_NAME, typeof(SCMUProjectMemberList), SPEventReceiverType.ItemUpdated);

            Web.Update();
        }
        catch (Exception)
        {
            throw;
        }
    }

public void AddEventReceiverToList(SPWeb web, string listName, Type eventReceiverClass, SPEventReceiverType eventType)
    {
        SPList list = this.GetListByName(web, listName);            

        string className = eventReceiverClass.FullName;
        string assemblyName = Assembly.GetAssembly(eventReceiverClass).FullName;

        list.EventReceivers.Add(eventType, assemblyName, className);
    }

2 个答案:

答案 0 :(得分:0)

如果您想为单个列表运行事件接收器..

参考Here

检查帖子的结尾,在Elements.xml中将属性更改为“ListTemplateId”为“ListURL”

答案 1 :(得分:0)

在Elements.xml文件中替换:

<Receivers ListTemplateId="100">

<Receivers ListUrl="Lists/Your List Name">