1

I need to create an item updating event for an existing list in an existing solution I just checked and there is already a feature for that, the question here is for this type of upgrade do I need to create feature upgrading?

Or only updating the solution + creating the code will do the trick?

2 Answers 2

3

You should use Feature Upgrading to register the new EventReceiver.

Just creating the code will not tell SharePoint to call the EventReceiver you have to do that in either an Elements.xml file using <Receivers> element or through code by creating a new element in the right EventReceivers collection

For the following examples assume you're upgrading to verion 7.0.0.0.

CAML
To add the event receiver in CAML your feature.xml should be something like:

<?xml version="1.0" encoding="utf-8" ?>
<Feature ... Version="7.0.0.0">
  <UpgradeActions>
    <VersionRange BeginVersion="0.0.0.0" EndVersion="7.0.0.0">
      <ApplyElementManifests>
        <ElementManifest Location="MyNewEventReceiver\Elements.xml" />
      </ApplyElementManifests>
    </VersionRange>
  <UpgradeActions>
  ...
  <ElementManifest Location="MyNewEventReceiver\Elements.xml" />
</Feature>

With the new Elements.xml being applied both on upgrade and on new activations

Code
To add the event receiver in code your feature.xml should be something like:

<?xml version="1.0" encoding="utf-8" ?>
<Feature ...   ReceiverAssembly="XXX" ReceiverClass="YYY" Version="7.0.0.0">
  <UpgradeActions>
    <VersionRange BeginVersion="0.0.0.0" EndVersion="7.0.0.0">
      <CustomUpgradeAction Name="AddNewEventReceiver" />
    </VersionRange>
  <UpgradeActions>
  ...
</Feature>

And in the FeatureReceiver you'll have code like:

public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
{
  SPWeb parentWeb = (SPWeb)properties.Feature.Parent;
  switch (upgradeActionName)
  {
    case "AddNewEventReceiver":
      // Add EventReceiver;
      break;
    ...
  }
}

public override virtual void FeatureActivated(SPFeatureReceiverProperties properties)
{
  SPWeb parentWeb = (SPWeb)properties.Feature.Parent;
  ...
  // Add EventReceiver;
  ...
}

public override virtual void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
  SPWeb parentWeb = (SPWeb)properties.Feature.Parent;
  ...
  // Remove EventReceiver;
  ...
}

With the code for adding the EventReceiver being run on Upgrade with ActionName = "AddNewEventReceiver" as well as on new activations. And of course removing it on deactivation.

To add an eventReceiver in code use something like:

parentWeb.Lists[_listName].EventReceivers.Add(_eventType, Assembly.GetExecutingAssembly().FullName, _eventReceiverClassName);
3
  • how can I register an event receiver in the feature upgrading event? and Yes I have read many times chris o brien posts, but never had to do an upgrade like this. thats why my new itemupdated event is not being fired :) Aug 2, 2012 at 13:37
  • See updated answer Aug 2, 2012 at 14:00
  • +1 for the good answer :) Aug 2, 2012 at 14:18
0

FeatureUpgrading is fired when a Feature is entering the Upgrading state, and this is triggered only if the Feature is due for upgrade (ie: the installed version is greater than that of the activated version).

Have a read through Chris O'Brien's blog series about Feature Upgrade to gain more understanding how Feature Upgrade works: http://www.sharepointnutsandbolts.com/2010/06/feature-upgrade-part-1-fundamentals.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.