Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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);
share|improve this answer
    
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 :) –  Esteban V Aug 2 '12 at 13:37
    
See updated answer –  Per Jakobsen Aug 2 '12 at 14:00
    
+1 for the good answer :) –  Esteban V Aug 2 '12 at 14:18

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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