R – How to update SPItemEventReceiver assembly version for a list in SharePoint

event-receiversharepoint

We have an SPItemEventReceiver compiled into its own assembly.

We are using STSDev to package up a SharePoint solution with this EventReceiver as a feature. I am not assigning the SPItemEventReceiver to a specific ListTemplateId within the elements.xml, but am instead linking a ReceiverAssembly in the feature.xml and programmaticaly assigning the SPItemEventReceiver to multiple SPList items.

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        foreach (SPWeb web in site.AllWebs)
        {
            SPListCollection webListCollection = web.Lists;

            foreach (SPList myList in webListCollection)
            {
                if (myList.Title == "Lab Reports")
                {
                    SPEventReceiverDefinitionCollection receivers = myList.EventReceivers;
                    SPEventReceiverDefinition receiver = receivers.Add();
                    receiver.Name = "PostUpdateLabReport";
                    receiver.Assembly = "LabReportEventHandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1111111111111";
                    receiver.Class = "LabReportEventHandlers.LabReportsHandler";
                    receiver.Type = SPEventReceiverType.ItemUpdated;
                    receiver.Update();
                    break;
                }
            }

            web.Dispose();
        }
    }

I am using FeatureDeactivating to do the reverse of the above code, removing the EventReceiver from the lists.

Question:

How should I handle the future event where LabReportEventHandlers is updated and the version changes?

These are the options I can think of:

  1. Deactivate / Reactivate feature — I would wrap the updated dll back into the SharePoint solution file, change my code above to reflect the new version, and use stsadmin to upgrade the solution. I would then deactivate/ reactivate the feature.

  2. Add Assembly redirection to the web.config.

  3. Don't bump the LabReportEventHandlers version number.

Is there something in changing the solution version that will help me?

I think there are problems with the 3 options:

  1. After deactivation of the feature, someone could update an item before I can reactiave.

  2. I would not want to edit the web.config by hand, so I would use the sharepoint API instead. Where would I run that code?

  3. This is just plain wrong, but easy.

Best Answer

Maybe you can encapsulate the logic that is prone to change into a separate assembly, that is referenced and used by your event handler. This way, the event handler itself won't change have to change, you would only deploy the updated "logic" assembly to the GAC or bin directory(ies) appropriately.

HTH, jt