Tuesday 4 February 2014

SharePoint 2013 - Updating features

Updating your existing features after deployment to the production farm is a common scenario in most of the SharePoint environments. SharePoint supports the concept of updating wsp's by using the Update-SPSolution cmdlet.

Update-SPSolution -Identity "MySolution.wsp" -LiteralPath "C:\My Projects\SP2013\MySolution\Packages\MySolution.wsp" -Local -GACDeployment

Updating the solution involves replacing the files and components with the latest versions including the latest assemblies in GAC. But solution upgradation does not involve updating the features with changes to existing items and adding new functionalities to the existing feature. For feature upgradation, you need to use the Update() method on the existing features after using the QueryFeatures() method to fetch the features to be upgraded.

Feature upgrade scenarios:

SharePoint foundation creates a feature instance when a feature is activated and tracks the metadata including the version number of the feature. The feature version number is a four-part number similar to .NET assembly versions.
To add a new element manifest to a feature upgrade action, you need to first define the version range element in the feature manifest xml and add the ApplyElementManifests elements with the newly added ElementManifest element. 

  <UpgradeActions>
    <VersionRange BeginVersion="1.0.0.0" EndVersion="3.0.0.0">
      <ApplyElementManifests>
        <ElementManifest Location="Controls\Elements.xml"/>
      </ApplyElementManifests>
    </VersionRange>
  </UpgradeActions>
</Feature>


SharePoint also allows adding custom code behind actions to run performing upgrade actions by using the FeatureUpgrading event receiver method. The values that are passed to this method can be mentioned as parameters in the feature manifest xml and included in the CustomUpgradeAction elements inside the UgradeActions element.
  <UpgradeActions>
    <VersionRange BeginVersion="1.0.0.0" EndVersion="3.0.0.0">      
      <CustomUpgradeAction Name="V3Upgrade">
        <Parameters>
          <Parameter Name="CustomerListNewName">
            Tenants
          </Parameter>
        </Parameters>
      </CustomUpgradeAction>
    </VersionRange>
  </UpgradeActions>
</Feature>

public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<stringstring> parameters)
{
    var web = properties.Feature.Parent as SPWeb;
    switch (upgradeActionName)
    {
        case "V3Upgrade":
            var listName = parameters["CustomerListNewName"];
            //code to perform action based on the value
            break;
        default:
            break;
    }
}


Performing the upgradation:

After updating the solution using the Update-SPSolution cmdlet, you have to make sure that the existing features are upgraded as well. For this you can query the features that need to be upgraded and call the update method on the feature definition as given below.

$featureId = New-Object System.Guid -ArgumentList "8bfc530d-d36e-4720-b0c0-a2edb3638810"
$webApplication = Get-SPWebApplication "http://mywebapplication.com"
$features = $webApplication.QueryFeatures($featureId, $true)
foreach($feature in $features)
{
$feature.Upgrade($true)

}

No comments:

Post a Comment