Pages

Saturday, November 20, 2010

SharePoint: Event Receiver and Assembly Versions

I had encountered an interesting problem with event receiver few days back. By default we are used to attach an event receiver class to a list by declaring the event receiver in an xml file as shown below:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="101">
    <Receiver>
      <Name>MyEverReceiverOnAdd</Name>
      <Type>ItemAdded</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>MyProject.MyEventReceiver, Version=2.1.0.0, Culture=neutral, PublicKeyToken=</Assembly>
      <Class>PermissionsEventHandler.PermissionsHandler</Class>
      <Data></Data>
      <Filter></Filter>
    </Receiver>
  </Receivers>
</Elements>

Figure 1: Event Receiver for version 2.1.0.0

As you can see in the xml file above we need to specify the version of the assembly.

 

Let’s image a scenario:

1. You have deployed your SharePoint solution (which has event receivers) while the assembly version is 2.1.0.0. So when you deployed the SharePoint solution, the list event receivers  in the SharePoint webs are registered with that 2.1.0.0 version of dll.

2. After few months, another version of your product is ready and you have changed the dll version (as this is practice to change your dll version as the development goes on). Now your dll version is 2.2.0.0. So you have updated the event receiver’s definition in xml file to reflect the new dll version as shown below:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="101">
    <Receiver>
      <Name>MyEverReceiverOnAdd</Name>
      <Type>ItemAdded</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>MyProject.MyEventReceiver, Version=2.2.0.0, Culture=neutral, PublicKeyToken=</Assembly>
      <Class>PermissionsEventHandler.PermissionsHandler</Class>
      <Data></Data>
      <Filter></Filter>
    </Receiver>
  </Receivers>
</Elements>

Figure 2:Event Receiver for version 2.2.0.0

3. Now if you deploy the latest version of dll in server, the old assembly will be removed from GAC.Now you can get two different behaviors:

  • New site: If you create a new site after version 2.2.0.0 is deployed, there’ll be no problem as the new site will register it’s event receiver with the updated xml (which is using 2.2.0.0.0 as shown in image 2).
  • Old site: Here’s your are in trouble. You attached the event receiver after the version 2.1.0.0 is deployed. So the lists will look for that version of dll in GAC. 

 

Solutions:

So how can you handle the situation? After googling and analyzing I have found three solutions:

  • Fixed Version: One solution might be to keep the event receiver classes in a project whose version will not be changed. So you’ll always refer to the an assembly with the same dll version.
  • Deactivate/Activate Old Features: Another solution might be to uninstall/Deactive and then install/activate the event receiver features for all old sites. But this will add extra overhead in deployment. Additionally, if the number of webs are larger then it’s not viable solution.
  • DotNet Publisher Policy: .net publisher policy can be another option to redirect the reference of old assembly to new one. More information on publisher policy can be found in the links: http://msdn.microsoft.com/en-us/library/dz32563a.aspx, http://support.microsoft.com/kb/891030

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.