List Receivers ListUrl property is not working

I recently faced an issue where List Event Receivers run for all lists and libraries. Then, the first thing I checked is the Event Receiver elements.xml and I found the Receivers element does have ListUrl property set. I’ve tried to add another “/” in front of the ListUrl property and it still does not work. You could see my list event receiver elements.xml below.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListUrl="Lists/MyList">
      <Receiver>
        <Name>MyListEventReceiverItemAdded</Name>
        <Type>ItemAdded</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>MyCustomList.MyListEventReceiver.MyListEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>MyListEventReceiverItemUpdated</Name>
        <Type>ItemUpdated</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>MyCustomList.MyListEventReceiver.MyListEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>MyListEventReceiverItemAdding</Name>
        <Type>ItemAdding</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>MyCustomList.MyListEventReceiver.MyListEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
</Receivers>
</Elements>

After spending some time to figure it out, I found this question from another forum and determine that Receivers ListUrl property DOES NOT WORK when the Feature scope is Site.
I’ve tested it again to change the feature scope to Web and ListUrl property works fine. But I really need the feature scope to be Site scope.

Therefore my solution is to Attach those Event Receivers when Feature activated. You could see great article to explain the list of steps here.

web.Lists["MyList"].EventReceivers.Add(SPEventReceiverType.ItemAdding, Assembly.GetExecutingAssembly().FullName, "MyCustomList.MyListEventReceiver.MyListEventReceiver");
web.Lists["MyList"].EventReceivers.Add(SPEventReceiverType.ItemAdded, Assembly.GetExecutingAssembly().FullName, "MyCustomList.MyListEventReceiver.MyListEventReceiver");
web.Lists["MyList"].EventReceivers.Add(SPEventReceiverType.ItemUpdated, Assembly.GetExecutingAssembly().FullName, "MyCustomList.MyListEventReceiver.MyListEventReceiver");

Tagged: , , , , , , , , , , , , ,

Leave a comment