Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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):

<XmlDocuments>
      <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events">
        <Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events">
          <Receiver>
            <Name>PlanetEventHandler</Name>
            <Type>ItemAdded</Type>
            <SequenceNumber>10000</SequenceNumber>
            <Assembly>UniverseEventHandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=841f382b41c47c60</Assembly>
            <Class>UniverseEventHandlers.PlanetItemEventReceiver</Class>
            <Data></Data>
            <Filter></Filter>
          </Receiver>
        </Receivers>
      </XmlDocument>
    </XmlDocuments>
share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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