18

I have two custom list definitions - List 1 and List 2, both with different ID's for the Type in the elements.xml. List 1 is called 'List1' and the other is called 'List 2', note that the second list has a space in the name meaning the url has the %20 characters in it.

I have created List Event Receivers for each of the custom lists, as I understand it you need to set the ListUrl of the tag in order to get the event receiver firing only for the list at that Url.

My problem is that even with that set to ListUrl="Lists/List%202" or ListUrl="Lists/List 2" the event receiver still runs for the other list, in fact both event receivers run for any list created on the site - e.g. Custom List.

How do I get them to run for a particular list only?! Is it the space that is causing the issue? Even the other event receiver for the list with no space fires for all lists!

I tried also setting the ListTemplateOwner to the feature ID but that didn't work either.

Thanks in advance,

Alex

2
  • Could you explain what you mean by the "ListUrl of the tag"? What tag? Could you post it up?
    – Andy Burns
    Commented Dec 24, 2010 at 13:05
  • I meant the ListUrl property of the Recievers element in the event receiver elements.XML file - sorry I didn't explain that very well
    – Anonymous
    Commented Dec 24, 2010 at 13:38

9 Answers 9

13

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

-Padeep Nulu

2
  • YESS!!! It works Paddep Nulu! You must create a feature with scope , so the parameter ListUrl = "Lists / Your List" ("elements.xml") works properly!! Very good!! Mauro
    – Mauro
    Commented May 21, 2012 at 18:02
  • You can also define your receiver scope in your elements.xml: <Receivers ListUrl="Lists/ListName" Scope="Web"> Commented Jan 3, 2014 at 18:53
13

If the feature scope is set to Site, then the ListUrl and the ListTemplateId is ignored, and then it will fire on all events for all lists.

Fix it by putting your eventreceiver in a web-scoped feature.

6

I had the same problem, and debugged for literally weeks. The solution I ended up with was deleting the elements.xml file that specifies the target of the event receivers, and binding the event receivers to your lists programmatically in the FeatureActivated (and don't forget to detach them in FeatureDeactivating).

Hope this helps someone.

1
  • Thanks - this is what I ended up having to do in the end - would be good to find out if this is actually a bug or feature though.
    – Anonymous
    Commented Feb 24, 2011 at 21:11
3

Specifying ListUrl should work in order to associate the event receiver with a particular list instance, i.e.

<Receivers ListUrl="Lists/test">

I have seen a problem where this didn't work for some reason, and the result was the event fired for all lists, as if the ListUrl attribute wasn't there. I don't think it was a typo, because the feature won't deploy if you get it wrong. For the same reason I don't think it is anything to do with there being spaces in your list name.

I created a new, clean project and created the event receiver and it worked fine. It might be worth trying the same thing in case there is some buggy behaviour around this when combined with other event receivers.

6
  • Ok thanks Bill for confirming - I'll try it in a new project to see if it plays ball. Just to verify should it be Lists/List%20Test to account for the space in the URL? Also if I want to register it by ListTemplateId instead I'm assuming this is the one in the List element of the list definition schema.XML? Thanks Alex
    – Anonymous
    Commented Dec 24, 2010 at 15:09
  • I think "Lists/List Test" and "Lists/List%20Test" both work - presumably it gets de-urlencodified somewhere in the process. If it doesn't match a list it should fail to deploy. The list template ID should be a number like 107 or something (Type or Type_Client I think).
    – SPDoctor
    Commented Dec 24, 2010 at 16:36
  • Well i had another go this morning - deleted all the event receivers from the project, did an IISRESET, re-created the site collection. Created only one event receiver with ListTemplateID, ListUrl properties set and still it runs for every list that is created. The only thing left to do is create a new project which I'm really not looking forward to doing. Any other ideas? Cheers
    – Anonymous
    Commented Dec 28, 2010 at 10:24
  • Just to confirm, it seemed to be creating a new project (actually a new solution) that got it working. New project/feature just had the event receivers for the lists. I would build a proj. with one event on one list and see how that goes. I was using the ItemAdding event.
    – SPDoctor
    Commented Dec 28, 2010 at 10:46
  • OK well I've now tried creating a new project in the same solution - still the same problem runs for all lists. I'm guessing the 'bug' must be at solution level?
    – Anonymous
    Commented Dec 28, 2010 at 11:17
3

For this,

You need to set scope of feature = Web as well as you need to set scope in Feature.Template.XML also. Basically, ListUrl property not works with Site Collection scope, it only works with Web scope.

You can see the solution here. http://asharepointsolutions.blogspot.in/2014/10/sharepoint-event-handler-firing-twice.html

1
  • this did the trick for me!
    – Snickbrack
    Commented Sep 13, 2016 at 7:52
0

That doesn't sound right - if you've got 2 List definitions then how can you specify the list URL? You've not created the list yet!

As your list definitions have different template IDs, you could register your handlers by list type. Indeed, if your lists were originally based on "Custom List", this could be why your receivers are running on Custom Lists - if you've registered them by template ID.

5
  • Thanks for your reply Andy, i see what your saying. I am creating the lists in the UI rather than using list instances - perhaps it doesn't know what list to register the events on at feature activation time. I tried to register it based on ListTemplateId - this not the same as the ID for the Type though. I set this I'm the list definition schema.XML and then in the elements for the event receiver but it still fired for all lists. Cheers
    – Anonymous
    Commented Dec 24, 2010 at 13:41
  • Yeah, you'll want Type (in the list element of schema.xml) to match ListTemplateId, along with the List Definition file too. I don't see how the registration can work when the lists aren't there. By registering against a list type, you don't need to worry so much. I take it that you've tried this in a new site? Just to be sure that you've not got old event registrations still kicking around?
    – Andy Burns
    Commented Dec 24, 2010 at 13:52
  • In an ideal world this would be true, but in my experience setting the TemplateID in the receiver XML doesn't actually work, the event's fire for all lists in the site anyway! (If anyone else has had a different experience let me know)
    – Neil White
    Commented Jan 12, 2011 at 15:06
  • Well it's my experience that setting the TemplateID makes the receiver fire only for lists of that template type.
    – Andy Burns
    Commented Jan 13, 2011 at 10:07
  • Thanks for your reply Neil - its reassuring to hear that other people have had the same issue too.
    – Anonymous
    Commented Jan 18, 2011 at 12:34
0

I've just had a go at this again - hoping that after installing SharePoint 2010 SP1 and VS SP1 this bug would be fixed. Sadly it seems to be still present - setting the ListUrl in the event receiver elements makes no difference and it runs for every list.

Is anyone else still experience this issue? perhaps a bug needs raising on the connect site?

Cheers

Alex

1
  • I just tested this and I was able to get it scoped to a particular list, using the names List1 and List 2. I used an ItemAdded event. Can you post your elements.xml that is causing the issue? Commented Sep 14, 2011 at 0:47
0

My situation was a bit different, but I landed here while searching for a solution.

I had a SharePoint list item event receiver scoped for Web and controlled by a Web-scoped feature. Thus, it should only fire on the website's list if the feature has been enabled. However, it was firing on other sites as well where the feature was deactivated.

Turned out that I had created a list template from a site where the feature was active. Upon creating the list from the template on other sites, the event receiver from the original site would be part of the template. Lesson learned: Don't create a list template while event receivers are active.

-2

Put only list name inside ListUrl property

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.