7

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$</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

Lukasz S
  • 648
  • 1
  • 10
  • 29
  • Do you mean it is fired for every list item for each and every list? – Ashish Patel Oct 25 '10 at 13:41
  • I mean that when some item is added to some list on the site (not necessarily list created from temlate 10200) the event is fired. – Lukasz S Oct 25 '10 at 13:53
  • Weird, sounds like you have done it right.. Try to activate the feature on a brand new site collection. Just to make sure it was not attached to all lists during the development. – Ashish Patel Oct 25 '10 at 14:10
  • Yes I've tried, nothing changed. – Lukasz S Oct 25 '10 at 14:51
  • Have you tried ListTemplateOwner = "GUID of your Feature" along with ListTemplateID. – Ashish Patel Oct 25 '10 at 15:31
  • Also for the unwanted lists (for which the event is being fired), can you check EventReceivers property (programatically or using using VS.NEt 2010's Server explorer) and see if your receiver is listed there? – Ashish Patel Oct 25 '10 at 15:39
  • Yes I've tried with GUID (no change). I've checked list instance EventReceivers - it's empty (checked in the ItemAdding Method) – Lukasz S Oct 25 '10 at 16:37

4 Answers4

13

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.

Rich Bennema
  • 10,295
  • 4
  • 37
  • 58
3

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

Marvin Pinto
  • 30,138
  • 7
  • 37
  • 54
Pradeep
  • 31
  • 1
2

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.

brian brinley
  • 2,364
  • 1
  • 13
  • 10
1

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