Question

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?

Was it helpful?

Solution

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);

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top