Welcome to weblogs.com.pk Sign in | Join | Help

VSeWSS – Sharepoint Event Receivers

Coding Event Receivers with Visual Studio Extensions for Windows Sharepoint Services (VSeWSS) is easy; you add it into your project using Add New Item –> Sharepoint / Event Receiver. You dont need to add “EventReceiver” in the name; as it will automatically post fix; ItemEventReceiver and ListEventReceiver names to the two classes that it generates. It also generates two associated XML files for events which VSeWSS uses and when deploying/packaging the VSeWSS generates two feature.xml files under PKG folder. Simply uncomment the event you need in the generated CS files and write your code into it. And instance of SPItemEventProperties is passed to each event.

  • Dont forget to call this.DisableEventFiring() before updating any list item; and this.EnableEventFiring() before exiting. Use try/finally for this!
  • You can use the display names of the field when using properties.ListItem, but you need to use the (Internal)Name of the field with properties.AfterProperties and properties.BeforeProperties
  • Usually the internal name of fields having spaces is different; the spaces are replaced with _x0020_
  • properties.ListItem always contain the current data of the list item for which event has been received; the BeforeProperties get populated for “ed” events; (Added, Updated, Deleted etc) and AfterProperties get populated for “ing” events; (Adding, Updating, Deleting etc)
  • There is some bug in Sharepoint List Eventing due to which properties.AfterProperties.ChangedProperties and/or properties.BeforeProperties.ChangedProperties doesnt give correct values; I didnt checked it myself; I just read it somewhere!

Deploying to plain Windows Sharepoint Services (WSS) works fine; but if you deploy your work to Sharepoint Portal Server (SPS); it doesnt; thats because in the ItemEventReceiver.xml file; the VSeWSS makes only the class entry and write the Guid of your class. For SPS; you need to enter not only assembly name but also the class name in .NET way. A sample entry is given below. Issue is whenever you create WSP file from VSeWSS; it will always append the entries of its format; so to have your WSP ready for SPS; you will need to edit the WSP file manually. The WSP file is basically a CAB file and you can use third party CAB tool to extract / replace the files in it.

<?xml version="1.0" encoding="utf-8"?>
<Elements Id="bd6deaa8-bb29-4c8f-8441-73ff0ea23007" xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateOwner="fbcf9ee9-6883-4443-8049-3d7c15de2dee">
    <Receiver>
      <Name>ItemAdded</Name>
      <Type>ItemAdded</Type>
      <SequenceNumber>1</SequenceNumber>
      <Class>MySharepointArtifacts.ExpenseQueueItemEventReceiver</Class>
      <Assembly>MySharepointArtifacts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=81c24f946f8d399f</Assembly>
    </Receiver>
  </Receivers>
</Elements>

Tips

  1. The wizard picks the lists in the VSeWSS project; if you havnt imported your list into VSeWSS using the solution generator (the tip I gave in my earlier post); its the good time to do it before adding your event receiver. 
  2. You can move the ItemEventReceiver.xml/cs and ListEventReceiver.xml/cs files to the folder hosting schema.xml and *.aspx files of your list; its good to have coupled things together under single folder
  3. You can remove the ItemEventReceiver.xml and ListEventReceiver.xml files all together; and instead of using VSeWSS for creating a feature for your Event Receiver; can use third party app to register your event receivers later once your assembly is on server.
    • u2u’ Event Handler Explorer is highly recommended; its WinForms app with source; you can even extract the event registering code from it and write it into some ASPX file if you already have FixMyList.aspx file for fixing Lookup fields; the tip I gave in my earlier post!
Published Monday, December 14, 2009 1:10 PM by khurram

Comments

# Here&#8217;s a walkthrough building an Event Receiver in VSeWSS | ASP Scribe

# Here's a walkthrough building an Event Receiver in VSeWSS

Monday, December 21, 2009 12:42 PM by Microsoft SharePoint Bloggers
New Comments to this post are disabled