SPFeature.Version property

Gets the current version of the Feature.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public Version Version { get; internal set; }

Property value

Type: System.Version
The version of the Feature.

The value of this property can be different from the value of the Version property of the underlying Feature definition. For example, you might deploy a Feature that uses a Feature definition with a version number of 1. At that point, both the Feature instance and the Feature definition have the same version number. Suppose that subsequently you modify the Feature definition and redeploy it with a version number of 2. Now the Feature definition is version 2, but the Feature instance remains version 1. The discrepancy indicates that the Feature instance needs to be upgraded. You need to run Feature upgrade code to change the version of the Feature instance from 1 to 2.

The following example gets a farm-wide collection that contains all instances of a Feature that need to be upgraded. The code then iterates through the collection and upgrades the Feature.

//  Represent the ID of the Feature we want to upgrade.
Guid featureId = new Guid("1B006A62-7B92-475c-A2E5-A1CF03EE0887");

//  Get the default Web service in the farm.
SPWebService webService = SPFarm.Local.Services.GetValue<SPWebService>("");

//  Get all Feature instances with the specified ID that require upgrade.
SPFeatureQueryResultCollection features = webService.QueryFeatures(featureId, true);

//  Get a Features enumerator.
IEnumerator<SPFeature> featureEnumerator = features.GetEnumerator();

while (featureEnumerator.MoveNext())
    //  Get current Feature.
    SPFeature feature = featureEnumerator.Current;

    //  Upgrade the current Feature.
    Console.WriteLine("Upgrading Feature {0} with ID {1}.", 
                      feature.Definition.DisplayName, feature.DefinitionId);
    Console.WriteLine("Feature Version Before Upgrade: {0}", feature.Version);
    Console.WriteLine("Feature Version After Upgrade: {0}", feature.Version);