Upgrading a feature property

27 Jan

In SharePoint 2010 features can be upgraded, awesome!
I was just testing this when I discovered something curious when updating a property.

Suppose the following feature definition file:

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/" Version="">
    <Property Key="MyProp" Value="initialValue" />

The property value of the MyProp key  is “initialValue”.

After adjusting the feature definition file to perform an upgrade the file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/" Version="">
    <VersionRange BeginVersion="" EndVersion="">
      <CustomUpgradeAction Name="UpgradeToOtherVersion"/>
    <Property Key="MyProp" Value="newValue" />

And I changed the property value of the MyProp key to “newValue”.
In the FeatureUpgrading event receiver I was checking the property value with the following statement:

SPFeatureProperty property1 = properties.Feature.Properties["MyProp"];
string initalValue = property1.Value;

and the result of property1.Value: “initialValue” ! I was suprised because I expected “newValue”.

After checking some other values and reading documentation on MSDN I came up with the following code to get the new property value:

SPFeatureProperty property2 = properties.Definition.Properties["MyProp"];
string updatedValue = property2.Value;

And the result:

The difference according to the documentation on MSDN is:
properties.Feature.Properties[“MyProp”] – Gets the collection of properties for the Feature Link to MSDN
properties.Definition.Properties[“MyProp”] – Gets a standardized property bag object that contains per-Feature settings that are specified in the Feature definition Link to MSDN

So I guess my conclusion is:
the property bag for the feature is updated to the new value after upgrading the feature and the property of the feature isn’t.

This sounds weird to me and I’m not sure I understand. Has anyone suggestions or experience with it?

While tweeting about this issue Chris O’Brien helped me out on this:
A feature has 1 definition and n instances. The code in FeatureUpgrading is used to upgrade the instances.
The property in the example updates the feature definition and not the running instances.
So properties.Feature.Properties[“MyProp”] gets the property value of the running instance and properties.Definition.Properties[“MyProp”] get the value of the property in the feature definition.

That’s it and not so weird after all! Thanks Chris!

One Reply to “Upgrading a feature property”

  1. Pingback: Tweets die vermelden Upgrading a feature property -- Topsy.com

Comments are closed.