You are on page 1of 3

24/04/12

How to: Create an Event Receiver for a Specific List Instance

How to: Create an Event Receiver for a Specific List Instance


Visual Studio 2010 A list instance event receiver responds to events that occur in any instance of a list definition. Although the event receiver template does not enable the targeting of a specific list instance, you can modify an event receiver that is scoped to a list definition to respond to events in a specific list instance. To target a specific list instance, in the Elements.xml for the event receiver, replace L s T m l t I with L s U land add the URL of the list instance. itepaed itr

Creating a List Instance Event Receiver


The following steps show how to modify a list item event receiver to respond only to events that occur in a custom announcements-list instance.

To modify an event receiver to respond to a specific list instance


1. Open the SharePoint site in a browser. 2. In the navigation pane, click Lists and then click Create. 3. In the Create dialog box, click Announcements, name the announcement TestAnnouncements, and then click Create. 4. In Visual Studio, create an event receiver project. 5. In the What type of event receiver do you want? list, select List Item Events. Note You can also select any other kind of event receiver that scopes to a list definition, for example, List Email Events or List Workflow Events.

6. Select Announcements as the event source, click An item is being added in the events list, and then click Finish. 7. In Solution Explorer, under EventReceiver1, double-click Elements.xml to open it. The event receiver currently references the Announcements list definition by using the line

<eevr LsTmltI=14> Rcies itepaed"0" Change this line to

<eevr LsUl"it/etnoneet" Rcies itr=LssTsAnucmns> This directs the event receiver to respond only to events that occur in the new TestAnnouncements announcements list that you just created. You can change the L s U Lattribute to reference any list instance on the SharePoint server. itR 8. Open the code file for the event receiver and put a breakpoint in the ItemAdding method.
msdn.microsoft.com/en-us/library/ff398052(d=printer).aspx 1/3

24/04/12

How to: Create an Event Receiver for a Specific List Instance

9. Press F5 to build and run the solution. 10. In SharePoint, click TestAnnouncements in the navigation pane. 11. Click the Add new announcement link. 12. Type a name for the announcement and then click Save. Notice that the breakpoint is hit when the new item is added to the custom announcements list. 13. Press F5 to resume. 14. In the navigation pane, click Lists and then click Announcements. 15. Add a new announcement. Notice that the event receiver does not trigger on the new announcement because the receiver is configured to respond only to events in the custom announcement list instance, TestAnnouncements.

See Also
Tasks How to: Create an Event Receiver1 Other Resources Developing SharePoint Solutions2

Links Table
1http://msdn.microsoft.com/en-us/library/ee231563.aspx 2http://msdn.microsoft.com/en-us/library/ee231517.aspx

Community Content
If you are not seeing the Event Receiver in the collection in Powershell
The feature where you are deploying the Event Receiver targeted to a specific list needs to be "Web" scoped, other wise it gets applied to all the lists and will not be available in the Event Receiver collection of the list. 3/27/2012 Chanchal J

Add more information


Please add this information in step 3. If the Announcement list is not available, you need to enable Team Collaboration Lists feature for the site: Go to Site Actions > Site Settings > Manage site features (under Site Actions) Activate the Team Collaboration Lists feature

msdn.microsoft.com/en-us/library/ff398052(d=printer).aspx

2/3

24/04/12

How to: Create an Event Receiver for a Specific List Instance

Event Reciever targetted to specific list.

2/29/2012 Ethand

2/5/2012 Hossein Aarabi

The following code snippet shows the elements.xml file for an event receiver for a customlist. Basically there are three more attributes added for <Receivers> element and those are: Scope: we can define the scope of Event Receiver to SiteCollection (Site) or Web level RootWebOnly: event receiver will be attached to all lists under root web created using particular template ListUrl: we can specify particular list on which Event Receiver will be active (/Lists/MyList/)
<eevr LsTmltI=10 LsUl"LssCsoLs/> Rcies itepaed"0" itr=/it/utmit" <eevr Rcie> <ae<Nm> Nm>/ae <ye<Tp> Tp>/ye <seby<Asml> Asml>/seby <ls>/ls> Cas<Cas <euneubr<SqecNme> SqecNme>/euneubr <Rcie> /eevr <Rcies /eevr>

2/29/2012 Ethand

4/18/2011 Nikunj Kalyani

2012 Microsoft. All rights reserved.

msdn.microsoft.com/en-us/library/ff398052(d=printer).aspx

3/3

You might also like