Every SharePoint Feature has a version number(by default “0.0.0.0“) that is specified in the corresponding Feature.xml file. When a Feature is activated at a specified scope, a Feature instance is created that is associated with that version of the Feature. Feature versioning in SharePoint Foundation allows Features and their associated instances to be easily tracked. Then, when you deploy a new version of a Feature, SharePoint Foundation detects that the associated Feature instance also needs an upgrade because the instance version number is lower than the new version number specified in the current Feature.xml file.
SharePoint Foundation uses new QueryFeatures methods that can be applied to top-level objects, such as SPWebApplication and SPSite, to determine what Feature instances need to be upgraded, based on their version numbers. The upgrade infrastructure queries for the set of Feature instances that need upgrade and then upgrades each of those Feature instances. The order in which the up-gradation happens is as follows:
- Server farm level,
- Web application level
- Site collection
- Web sites level(starting from root web level to child level webs)
We can perform the following kinds of modular upgrades to Features in SharePoint Foundation:
- Define upgrade definitions for new Feature versions.
- Provision a list instance as part of a Feature upgrade
- Create separate upgrade action sets, based on the Feature version, that remove different sets of files.
- Apply settings to site collections where a particular Feature is activated.
Feature receivers (SPFeatureReceiver) can now be used to handle FeatureUpgrading(SPFeatureReceiverProperties, String, IDictionary(Of String, String)) events. You can implement your own custom Feature receiver to upgrade Feature instances.
FeatureUpgrading(SPFeatureReceiverProperties, String, IDictionary(Of String, String)) parameters include properties of the current executing context, the name of the custom upgrade action to execute, and a dictionary of custom upgrade action parameters.
In order to perform the custom action we must add a feature <UpgradeActions> section in a Feature.xml file. Below is the example of this tag
<!-- Here you specify other upgrade actions to apply to Feature instances whose versions are within the range 220.127.116.11 to 18.104.22.168 -->
- <CustomUpgradeAction> — This is used to execute custom code while feature is being upgraded.
- <VersionRange> — Specifies a version range to which specified upgrade actions apply.
- <ApplyElementManifests> — Adds a new element to an existing Feature.
- <AddContentTypeField> — Adds a new field to an existing provisioned content type.
- <MapFile> — Allows you to map an uncustomized file to a different location on the front-end Web server. You can use the FromPath and ToPath attributes to rename a file in a Feature (for example, <MapFile FromPath=”oldname.gif” ToPath=”newname.gif” />, but you can also use MapFile to move a file. In this case, the FromPath and ToPath attributes specify paths that are relative to the TEMPLATE directory. For example, if a Feature named “MyFeature” has .gif files installed in a “Gifs” directory (such as, %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\FEATURES\MyFeature\Gifs\basketball.gif), and in version 2 you want to rename the directory from “Gifs” to “Images”, then <MapFile FromPath=”Gifs\ball.gif” ToPath=”Images\basketball.gif” /> can be used to move the files.
Things to be remembered:
- Increase the version number when you update a Feature.
- Keep version numbers of your Feature independent from Microsoft product versions
- Increment a major version number to the current major product version the first time you create or change a Feature during a new development cycle
- Increment the build version for subsequent changes during a development cycle.
- For an initial service pack change, increment the minor version.
Adding new elements to an existing Feature:
- Add a Version attribute
- Add an <UpgradeActions> section
- Create a new Elements.xml file that contains all the new elements
- Reference the Elements2.xml file in both the main <ElementManifests> section and the <ApplyElementManifests> section within the <UpgradeActions> section.
ReceiverAssembly="MyFeatureReceiver, Version=22.214.171.124, Culture=neutral, PublicKeyToken=3ef91b1292056a22"
Error Handling: When an error occurs during upgrade, the upgrade stops for the specified Feature instance and the error is recorded, both in ULS logs and also in an Upgrade.log file.
There is very good tool you can use to upgrade the feature created by Chris O’Brien. You can download and deploy this tool from codeplex
For more reference: feature upgrade