Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I add event receiver only for list with specyfic template id

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

like image 983
Lukasz S Avatar asked Oct 25 '10 11:10

Lukasz S


People also ask

How do I add an event receiver to a SharePoint list?

Select event receiver and enter the title of the event receiver. Click the Add button. Select List Item Events in the type of event receiver and select the custom list option from the event source. Then select "an item was added" in events and click Finish.

What are the type of event receivers available in SharePoint list?

There are two types of Event Receiver in SharePoint, Synchronous Event Receiver. Asynchronous Event Receiver.

How do I make a remote event receiver?

To register a remote event receiverOn the menu bar, select Project > Add New Item. In the Installed Templates pane, select the Office/SharePoint node. In the Templates pane, select the Remote Event Receiver template. In the Name box, keep the default name (RemoteEventReceiver1), and then select Add.


1 Answers

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.

like image 55
Rich Bennema Avatar answered Nov 07 '22 06:11

Rich Bennema