Partilhar via


SPFeature.Version Property

Gets the current version of the Feature.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Property Version As Version
    Get
    Friend Set
'Usage
Dim instance As SPFeature
Dim value As Version

value = instance.Version
public Version Version { get; internal set; }

Property Value

Type: System.Version
The version of the Feature.

Remarks

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.

Examples

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);
    feature.Upgrade(false);
    Console.WriteLine("Feature Version After Upgrade: {0}", feature.Version);
}
'  Represent the ID of the Feature we want to upgrade.
Dim featureId As New Guid("1B006A62-7B92-475c-A2E5-A1CF03EE0887")

'  Get the default Web service in the farm.
Dim webService As SPWebService = SPFarm.Local.Services.GetValue(Of SPWebService)("")

'  Get all Feature instances with the specified ID that require upgrade.
Dim features As SPFeatureQueryResultCollection = webService.QueryFeatures(featureId, True)

'  Get a Features enumerator.
Dim featureEnumerator As IEnumerator(Of SPFeature) = features.GetEnumerator()

Do While featureEnumerator.MoveNext()
    '  Get current Feature.
    Dim feature As SPFeature = 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)
    feature.Upgrade(False)
    Console.WriteLine("Feature Version After Upgrade: {0}", feature.Version)
Loop

See Also

Reference

SPFeature Class

SPFeature Members

Microsoft.SharePoint Namespace

Definition

Version