如何仅为具有特定模板 ID 的列表添加事件接收器

发布于 2024-09-29 11:17:07 字数 740 浏览 8 评论 0原文

我正在为自定义列表模板添加 ItemAdding 事件接收器。事件接收器和列表模板都是由相同的功能部署的。同样的功能还可以创建列表实例。

我遇到的问题是,该事件在部署到的站点中为每个列表项触发。 eventreceivre 的 Elements.xml 是:

<Receivers ListTemplateId="10200">
  <Receiver>
    <Name>ListEventReceiverItemAdding</Name>
    <Type>ItemAdding</Type>
    <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
    <Class>SharepoitProject.ListEventReceiver</Class>
    <SequenceNumber>10000</SequenceNumber>
  </Receiver>
</Receivers>

我不确定我做错了什么我已经或多或少地完成了 此处

Env:Sharepoint 2010,在此网站上具有发布功能

I'm adding ItemAdding event receiver for custom list template. Both event receiver and list template are deployed by the same feature. Also the same feature creates List Instances.

The problem I've got is that the event is fired for each list item in site to which it was deployed. Elements.xml for eventreceivre is:

<Receivers ListTemplateId="10200">
  <Receiver>
    <Name>ListEventReceiverItemAdding</Name>
    <Type>ItemAdding</Type>
    <Assembly>$SharePoint.Project.AssemblyFullName
lt;/Assembly>
    <Class>SharepoitProject.ListEventReceiver</Class>
    <SequenceNumber>10000</SequenceNumber>
  </Receiver>
</Receivers>

I'm not sure what I'm doing wrong I've done more or less everythin from here.

Env: Sharepoint 2010 with Publishing Feature on this Site

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

记忆消瘦 2024-10-06 11:17:07

我的事件接收器也有同样的问题。我尝试了ListTemplateId、ListTemplateOwner,甚至ListUrl。我知道这些设置是有效的,但它们被忽略了,并且接收器被附加到每个列表中。

我有预感,这可能与在站点范围功能中声明的事件接收器有关。 SPEventElement 的 "documentation" 似乎证实了这一点:

  switch (this.FeatureDefinition.Scope)
  {
    case SPFeatureScope.Site:
      if (this.SiteScopedReceivers())
      {
        this.UpdateEventReceiversForSite(site, sqlcmdAppendOnly, new SPEventElement.GetSqlToUpdateEventReceiversForSite(this.GetEventReceivers(site.RootWeb).GetSqlToAddEventReceiversToSite));
        break;
      }
      else
      {
        this.UpdateEventReceiversForWeb(site.RootWeb, sqlcmdAppendOnly, new SPEventElement.GetSqlToUpdateEventReceiversForWeb(this.GetEventReceivers(site.RootWeb).GetSqlToAddEventReceiversToWeb));
        break;
      }
    case SPFeatureScope.Web:
      if (this.RootWebOnly && !web.IsRootWeb)
      {
        ULS.SendTraceTag(1718513714U, (ULSCatBase) ULSCat.msoulscat_WSS_General, ULSTraceLevel.Verbose, "Event Receivers in Feature '{0}' were not activated because current web is not the root web.", new object[1]
        {
          (object) this.FeatureDefinition.Id.ToString("B")
        });
        break;
      }
      else
      {
        bool templateIdExists;
        int templateId;
        this.CheckTemplateId(out templateIdExists, out templateId);
        if (!templateIdExists)
        {
          if (this.ListUrl != null)
          {
            this.UpdateEventReceiversForList(web, sqlcmdAppendOnly, new SPEventElement.GetSqlToUpdateEventReceiversForList(this.GetEventReceivers(web).GetSqlToAddEventReceiversToList), true);
            break;
          }
          else
          {
            this.UpdateEventReceiversForWeb(web, sqlcmdAppendOnly, new SPEventElement.GetSqlToUpdateEventReceiversForWeb(this.GetEventReceivers(web).GetSqlToAddEventReceiversToWeb));
            break;
          }
        }
        else
        {
          if (this.ListUrl != null)
            throw new SPException(SPResource.GetString("ElementHasBothTemplateIdAndUrl", new object[0]));
          this.UpdateEventReceiversForListTemplate(templateId, web, sqlcmdAppendOnly, new SPEventElement.GetSqlToUpdateEventReceiversForList(this.GetEventReceivers(web).GetSqlToAddEventReceiversToList));
          break;
        }
      }
  }

对于站点范围,ListTemplateId、ListTemplateOwner 和 ListUrl 似乎被忽略特征。当我将事件接收器元素移动到 Web 范围的功能时,接收器仅正确附加到所需的列表。

I had the same problem with my Event Receiver. I tried ListTemplateId, ListTemplateOwner, and even ListUrl. I knew the settings were valid, but they were being ignored and the receiver was being attached to every list.

I had a hunch that it might be related to the Event Receiver being declared within a Site scoped feature. This seems to be confirmed by the "documentation" for SPEventElement:

  switch (this.FeatureDefinition.Scope)
  {
    case SPFeatureScope.Site:
      if (this.SiteScopedReceivers())
      {
        this.UpdateEventReceiversForSite(site, sqlcmdAppendOnly, new SPEventElement.GetSqlToUpdateEventReceiversForSite(this.GetEventReceivers(site.RootWeb).GetSqlToAddEventReceiversToSite));
        break;
      }
      else
      {
        this.UpdateEventReceiversForWeb(site.RootWeb, sqlcmdAppendOnly, new SPEventElement.GetSqlToUpdateEventReceiversForWeb(this.GetEventReceivers(site.RootWeb).GetSqlToAddEventReceiversToWeb));
        break;
      }
    case SPFeatureScope.Web:
      if (this.RootWebOnly && !web.IsRootWeb)
      {
        ULS.SendTraceTag(1718513714U, (ULSCatBase) ULSCat.msoulscat_WSS_General, ULSTraceLevel.Verbose, "Event Receivers in Feature '{0}' were not activated because current web is not the root web.", new object[1]
        {
          (object) this.FeatureDefinition.Id.ToString("B")
        });
        break;
      }
      else
      {
        bool templateIdExists;
        int templateId;
        this.CheckTemplateId(out templateIdExists, out templateId);
        if (!templateIdExists)
        {
          if (this.ListUrl != null)
          {
            this.UpdateEventReceiversForList(web, sqlcmdAppendOnly, new SPEventElement.GetSqlToUpdateEventReceiversForList(this.GetEventReceivers(web).GetSqlToAddEventReceiversToList), true);
            break;
          }
          else
          {
            this.UpdateEventReceiversForWeb(web, sqlcmdAppendOnly, new SPEventElement.GetSqlToUpdateEventReceiversForWeb(this.GetEventReceivers(web).GetSqlToAddEventReceiversToWeb));
            break;
          }
        }
        else
        {
          if (this.ListUrl != null)
            throw new SPException(SPResource.GetString("ElementHasBothTemplateIdAndUrl", new object[0]));
          this.UpdateEventReceiversForListTemplate(templateId, web, sqlcmdAppendOnly, new SPEventElement.GetSqlToUpdateEventReceiversForList(this.GetEventReceivers(web).GetSqlToAddEventReceiversToList));
          break;
        }
      }
  }

It appears that ListTemplateId, ListTemplateOwner, and ListUrl are ignored for Site scoped features. When I moved my Event Receiver element to a Web scoped feature, the receiver was properly attached only to the desired list.

难理解 2024-10-06 11:17:07

当范围限定为网站时,它会针对所有列表触发。当我将解决方案范围限定为网络时,它起作用了。希望有帮助。

When scoped to site it fired for all lists. When I scoped the solution to web it worked. Hope it helps.

喵星人汪星人 2024-10-06 11:17:07

来自 MSDN SDK - http://msdn.microsoft.com/en-us/库/ms431081.aspx


接收者标签可以暗示站点范围的
活动注册或活动
根 Web 的注册。这
范围属性用于定义
事件接收者是什么级别
应用。如果接收者标签没有
ListTemplateId 或 ListUrl 属性,
事件接收器注册于
与功能的范围相同。为了
例如,一个功能的范围是
Web 结果产生事件接收器
被添加到事件接收器
范围仅限于 Web 的集合。


这让我相信您提供的列表 templateID 可能无效。

From the MSDN SDK - http://msdn.microsoft.com/en-us/library/ms431081.aspx


A Receivers tag can imply a site-wide
event registration or an event
registration for the root Web. The
Scope attribute is used to define at
what level the event receivers are
applied. If the Receivers tag has no
ListTemplateId or ListUrl attribute,
the event receiver is registered at
the same scope as the Feature. For
example, a Feature that is scoped to
the Web results in an event receiver
being added to an event receiver
collection that is scoped to the Web.


This leads me to believe that the list templateID you supplied may be invalid.

梦途 2024-10-06 11:17:07

如果您只需要特定列表的事件接收器范围,则必须设置此属性

它可以工作 ISA
但请确保您的功能是 Web 范围而不是站点范围,就好像它是站点范围一样,它将针对系统中的所有列表触发

If you need too scope the event receiver for specific list only you must set this attribute

and it will work ISA
but make sure that your feature is web scoped not site scoped as if it's site scope it will fire for all lists in the system

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文