So I have a question:

I have an event receiver, that should be hired for a specific list. So I changed the ListUrl property on the elements.xml of the event receiver. But now I need the same event receiver in another list. Is there a way to configure the other list ListUrl in the same way? I don't want this event receiver to hire on all my custom lists


Or alternatively, you could attach the event receiver to a Content Type, which you would create and use it as the basis for your 2 (or more) lists. You get more flexibility in the future as the only thing to is simply create a custom list and attach your content type. SharePoint will do the attaching for you, so even if you rename the list, it would still work without recompilation!

Steps would be:

  1. Create your own Content Type (inherit the one closest to your target definition in terms of columns, data-types, etc. you need to that at the Sub-Site or Top-Level site level depending on the scope you plan to give it - the whole site collection or just a sub-site and its subsites)

  2. Using the Name of that content type (although to have it all packed i would include the CT in the same solution), in your solution use techniques as explained here to attach it http://karinebosch.wordpress.com/walkthroughs/event-receivers-walkthrough3/. The part you are looking for is (where you could even use Tokens for your Assembly and Class to let Visual Studio replace them for you on compilation):

      <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events">
        <Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events">
            <Assembly>UniverseEventHandlers, Version=, Culture=neutral, PublicKeyToken=841f382b41c47c60</Assembly>

You have two options, either remove the ListUrl and let the Event Receiver fire for all the custom lists and inside your event through code check if the List is the one you want, then move on else ignore, something like:

if (properties.ListTitle == "MyCustomList1" || properties.ListTitle == "MyCustomList2")
   //your code

If you want to check ID of the list, then properties.ListItem.ParentList will give you SPList Object and then you can check properties.ListItem.ParentList.ID

Or you can add other Event Receiver in same solution with ListUrl, make a separate class inside the solution, move your code logic in a function in this separate class, call the function of this class from both Event Receivers... To manage a centralized code!

I hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.