Monday 25 November 2013

Create an Event Receiver for a Specific List Instance

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. In a browser, open the SharePoint site.
  2. In the navigation pane, Lists link.
  3. In the All Site Content page, choose the Create link.
  4. In the Create dialog box, choose the Announcements type, name the announcement TestAnnouncements, and then choose the Create button.
  5. In Visual Studio, create an event receiver project.
  6. In the What type of event receiver do you want? list, choose List Item Events.
    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.
  7. In the What item should be the event source? list, choose Announcements.
  8. In the Handle the following events list, select the An item is being added check box, and then choose the Finish button.
  9. In Solution Explorer, under EventReceiver1, open Elements.xml.
    The event receiver currently references the Announcements list definition by using the following line:
The event receiver currently references the Announcements list definition by using the following line:

<Receivers ListTemplateId="104">
 
Change this line to the following text:

<Receivers ListUrl="Lists/TestAnnouncements">

  1. 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 ListURL attribute to reference any list instance on the SharePoint server.
  2. Open the code file for the event receiver and put a breakpoint in the ItemAdding method.
  3. Choose the F5 key to build and run the solution.
  4. In SharePoint, choose the TestAnnouncements link in the navigation pane.
  5. Choose the Add new announcement link.
  6. Enter a title for the announcement, and then choose the Save button.
  7. Notice that the breakpoint is hit when the new item is added to the custom announcements list.
  8. Choose the F5 key to resume.
  9. In the navigation pane, choose the Lists link, and then choose the Announcements link.
  10. Add a new announcement.
  11. 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.

No comments:

Post a Comment