|
Event Receivers are triggered by certain events on either a SPWeb, SPList or SPListItem. Event Receivers can be attached by either using feature elements definitions or by using the object model (using Feature Receivers). The event handler method that are attached are defined in a separate class Event Receiver class that implements either SPWebEventReceiver, SPListEventReceiver, SPItemEventReceiver or SPEmailEventReceiver. Creating an Event Receiver classThe following code is recommended as a base for Event Receivers to ensure a few things:
public class ScheduleListEventReceiver : SPItemEventReceiver { public override void ItemAdding(SPItemEventProperties properties) { System.Diagnostics.Debug.WriteLine("ScheduleListEventReceiver.ItemAdding(): begin"); try { this.DisableEventFiring(); //TODO: do stuff! base.ItemAdding(properties); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("ScheduleListEventReceiver.ItemAdded() exception" + ex.ToString()); } finally { this.EnableEventFiring(); } System.Diagnostics.Debug.WriteLine("ScheduleListEventReceiver.ItemAdding(): end"); } public override void ItemAdded(SPItemEventProperties properties) { System.Diagnostics.Debug.WriteLine("ScheduleListEventReceiver.ItemAdded(): begin"); try { this.DisableEventFiring(); //TODO: do stuff! base.ItemAdded(properties); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("ScheduleListEventReceiver.ItemAdded() exception" + ex.ToString()); } finally { this.EnableEventFiring(); } System.Diagnostics.Debug.WriteLine("ScheduleListEventReceiver.ItemAdded(): end"); } public override void ItemUpdating(SPItemEventProperties properties) { System.Diagnostics.Debug.WriteLine("ScheduleListEventReceiver.ItemUpdating(): begin"); try { this.DisableEventFiring(); //TODO: do stuff! base.ItemUpdating(properties); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("ScheduleListEventReceiver.ItemUpdating() exception" + ex.ToString()); } finally { this.EnableEventFiring(); } System.Diagnostics.Debug.WriteLine("ScheduleListEventReceiver.ItemUpdating(): end"); } public override void ItemUpdated(SPItemEventProperties properties) { System.Diagnostics.Debug.WriteLine("ScheduleListEventReceiver.ItemUpdated(): begin"); try { this.DisableEventFiring(); //TODO: do stuff! base.ItemUpdated(properties); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("ScheduleListEventReceiver.ItemUpdated() exception" + ex.ToString()); } finally { this.EnableEventFiring(); } System.Diagnostics.Debug.WriteLine("ScheduleListEventReceiver.ItemUpdated(): end"); } } Updating item properties in Event ReceiverThis one burnt up hours of my time...on the properties (SPItemEventProperties) parameter passed in there are various objects inside it. The misleading on is the .ListItem one. If you want to change a property on the ListItem please be aware that you need to use the .AfterProperties and then let the base method call actually do the update. If you try and call properties.ListItem.Update() within your event receiver you will receive errors such as Save Conflicts. Cancelling EventsYou can cancel event receivers which will provide a generic SharePoint error page using the following code. This is often used as a serverside way of validating submissions. 01: properties.ErrorMessage = "You cannot save this list item at this time"; 02: properties.Cancel = true; Using feature manifest to attach Event Receiver to ListDeployment scope: Feature applies to Web (web site). <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Receivers ListTemplateId="100"> <Receiver> <Name>ScheduleListEventReceiver</Name> <Type>ItemAdded</Type> <SequenceNumber>10000</SequenceNumber> <Assembly>jeremythake.tvshowschedulesite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8eb9a930004f2f1a</Assembly> <Class>jeremythake.tvshowschedulesite.ScheduleListEventReceiver</Class> <Data></Data> <Filter></Filter> </Receiver> <Receiver> <Name>ScheduleListEventReceiver</Name> <Type>ItemUpdated</Type> <SequenceNumber>10000</SequenceNumber> <Assembly>jeremythake.tvshowschedulesite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8eb9a930004f2f1a</Assembly> <Class>jeremythake.tvshowschedulesite.ScheduleListEventReceiver</Class> <Data></Data> <Filter></Filter> </Receiver> </Receivers> </Elements> Using object model to attach Event Receiver to ListDeployment scope: Programmatically applied per list. 01: web.Lists["Schedule"].EventReceivers.Add( 02: SPEventReceiverType.ItemAdded, 03: "jeremythake.tvshowschedulesite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8eb9a930004f2f1a", 04: "jeremythake.tvshowschedulesite.ScheduleListEventReceiver"); 05: web.Lists["Schedule"].EventReceivers.Add( 06: SPEventReceiverType.ItemUpdated, 07: "jeremythake.tvshowschedulesite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8eb9a930004f2f1a", 08: "jeremythake.tvshowschedulesite.ScheduleListEventReceiver"); Can also be done a little bit more programmatically Assembly assembly = Assembly.GetExecutingAssembly(); SPEventReceiverDefinition eventReceiver = list.EventReceivers.Add(); eventReceiver.Name = "Policy of Truth"; eventReceiver.Type = SPEventReceiverType.ItemUpdated; eventReceiver.SequenceNumber = 200; eventReceiver.Assembly = assembly.FullName; eventReceiver.Class = "TST.POC.PolicyFeatures.PolicyOfTruthHandler"; eventReceiver.Update(); Source: Tom Stegeman Using Content Types to attach Event Receiver to ListsDeployment scope: Content type feature applies to Site (site collection). <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <ContentType ID="CONTENT TYPE ID" Name="CONTENT TYPE NAME" Group="Custom" Description="CONTENT TYPE DESCRIPTION" Version="0"> <FieldRefs> <FieldRef ID="FIELD ID" Name="FIELD NAME" /> </FieldRefs> <XmlDocuments> <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events"> <spe:Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events"> <Receiver> <Name>RatingSummaryEventHandler</Name> <Type>ItemAdded</Type> <SequenceNumber>10000</SequenceNumber> <Assembly>Dhunter.SharePoint.Eventhandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0a26195ff03df1fd</Assembly> <Class>Dhunter.SharePoint.Eventhandlers.RatingSummaryEventHandler</Class> <Data></Data> <Filter></Filter> </Receiver> <Receiver> <Name>RatingSummaryEventHandler</Name> <Type>ItemUpdated</Type> <SequenceNumber>10000</SequenceNumber> <Assembly>Dhunter.SharePoint.Eventhandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0a26195ff03df1fd</Assembly> <Class>Dhunter.SharePoint.Eventhandlers.RatingSummaryEventHandler</Class> <Data></Data> <Filter></Filter> </Receiver> </spe:Receivers> </XmlDocument> </XmlDocuments> </ContentType> </Elements> Source: Dave Hunter GotchasBeforeProperties and AfterProperties
Add for each ItemAdded, ItemUpdated etc. event not just once for the listWhen you are adding Event Receivers you need to remember that you have to add a EventRecevier for each SPEventReceiverType e.g. ItemAdded or ItemUpdated. Office 2007 documentsOffice 2007 documents cannot be modified in the ItemAdding Event (using AfterProperties) unless SPWeb.ParserEnabled = false. DebuggingDebugging an SPEmailEventReceiver requires attaching the debugger to the OWSTIMER.EXE process. This is because the timer service fires the Incoming E-mail timer job, rather than the SharePoint web application. ItemUpdating & ItemUpdated running twiceSource: chakkaradeep Here is the solution from Microsoft
if (properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null && properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] != null) { //This is when the update event is triggered by check-in. } else { //This is triggered by events other than check-in action. } ItemAdding AfterPropertiesThanks to Adam Toth properties.AfterProperties["Container Record Number"] = "blah"; base.ItemAdding(properties); Not this: properties.AfterProperties["Container_x0020_Record_x0020_Number"] = "blah"; base.ItemAdding(properties); Event Receivers on Content TypesSource: Steven Van de Craen
ItemDeleted missing vital information aboutSource: Einar Otto Stangvik External Links
Labels |







Comments (5)
May 21, 2009
Anonymous says:
I think there is a mistake in the introduction paragraph, in the following ...I think there is a mistake in the introduction paragraph, in the following phrase:
The event handler method that are attached are defined in a separate class Event Receiver class that implements either SPWebEventReceiver, SPItemEventReceiver, SPListItemEventReceiver or SPEmailEventReceiver
The bit in red should be SPListEventReceiver.
Jun 13, 2009
Keith Dahlby says:
Fixed.Fixed.
Jul 02, 2009
Anonymous says:
Another GOTCHA SPItemEventReceiver.BeforeProperties are only available on docum...Another GOTCHA
SPItemEventReceiver.BeforeProperties are only available on document libraries and not on custom lists. See below article for more GOTCHAS.
http://msdn.microsoft.com/en-us/library/aa979520.aspx
Sep 30
Anonymous says:
Hi Can you please tell how to Programmatically populate the WebParts into the ...Hi
Can you please tell how to Programmatically populate the WebParts into the gallery, which was previously deleted(programmatically ) from the “Webpart Gallery” list ?
With regards
Biju
Dec 14
Anonymous says:
Gotcha 1 The advice in this article for using AfterProperties in ItemAdding is...Gotcha 1
The advice in this article for using AfterProperties in ItemAdding is to use the Display Name of the field.
Currently in an SP1 32bit environment this doesn't seem to ring true! AfterProperties accessed via the Display Name return null. Using the Internal Name however works and the fields can be set accordingly.
Cotcha 2
In the provided ScheduleListEventReceiver example the base methods are called
e.g. base.ItemUpdating(properties).
Calling the base methods as illustrated can cause unwanted behaviour
i.e. calling the base method after setting 'properties.Cancel = true' seems to prevent it from cancelling
Either forget calling the base methods alltogether, or call them first, rather than last.