3

I created a farm feature and then I upgraded it.

According to the documentation, the upgrade-spfeature -scope farm will upgrade all features requiring upgrade. Feature upgrade (part 5) – using PowerShell to upgrade Features (this article)

I only want to ugprade one and it has to be with PowerShell.

Any idea?

4 Answers 4

7

im presuming that the feature is in a wsp so you should be able to do this:

Update-SPSolution –Identity YourSolutionName.wsp –LiteralPath “C:\YourSolutionName.wsp” –GacDeployment

http://technet.microsoft.com/en-us/library/ff607724.aspx

EDIT

forgot to mention you need to change the version number ;) for a more finer detail you can follow this:

1.Open the Feature Designer for Feature2. In the Properties pane, set the version number to 1.0.0.0. Where no version number is specified, a default of 0.0.0.0 is assumed. Version numbers must contain four components.

2.From the Build menu, select Deploy. This will deploy our version 1 solution to the farm.

3.In the Feature Designer, click the Manifest button at the bottom of the page. Expand the Edit Options section to display the Manifest Template. Replace the template XML with the following:

  <?xml version="1.0" encoding="utf-8" ?>
  <Feature xmlns="http://schemas.microsoft.com/sharepoint/">
  <UpgradeActions>
  <VersionRange>
  <CustomUpgradeAction Name="MyUpgrade"/>
  </VersionRange>
  </UpgradeActions>
  </Feature>

By attaching this XML to the feature definition, we’re defining the steps that should be taken to upgrade existing features. The CustomUpgradeAction element specifies that we’re using a feature receiver to perform the upgrade programmatically. In this example, we haven’t specified a version range, so this upgrade action will apply for all versions. If we needed to include different upgrade actions for different versions we could add this:

  <Feature xmlns="http://schemas.microsoft.com/sharepoint/">
    <UpgradeActions>
      <VersionRange BeginVersion="1.0.0.0" EndVersion="2.0.0.0">
        <CustomUpgradeAction Name="V2Upgrade"/>
      </VersionRange>
      <VersionRange BeginVersion="2.0.0.0" EndVersion="3.0.0.0">
        <CustomUpgradeAction Name="V3Upgrade"/>
      </VersionRange>
    </UpgradeActions>
  </Feature>

4.In the Properties pane, change the Version number for Feature 2 to 2.0.0.0.

Note

Within the Properties pane are options to set the Upgrade Actions Receiver Assembly and Class properties. These properties allow a feature to use a separate assembly for handling standard feature events such as Activate and Deactivate and a separate assembly for handling upgrade events. This facility is useful for retrofitting upgrade capabilities to a feature if the existing receiver assembly isn’t available or can’t be altered for some reason.

5.For the sake of simplicity, we’ll implement our upgrade code in our existing feature receiver. In the Feature2.EventReceiver.cs file, add the following code:

Code View: Scroll / Show All

public override void FeatureUpgrading(
                             SPFeatureReceiverProperties properties,
                             string upgradeActionName,
                             IDictionary<string, string> parameters)
    {
      switch (upgradeActionName)
      {
        case "MyUpgrade":
          if (properties.Feature.Parent is SPWeb)
          {
            SPWeb web = properties.Feature.Parent as SPWeb;
            using (Stream s = properties.Definition.GetFile(
                                              "FirstElement\\MyConfig.xml"))
            {
              using (XmlReader rdr = XmlReader.Create(s))
              {
                rdr.ReadToDescendant("List");
                do
                {
                  string listName = rdr.GetAttribute("name").ToString();
                  SPList myList = web.Lists.TryGetList(listName);

                  if (myList != null)
                  {
                   myList.Description += "- Updated";

                    myList.Update();
                  }
                } while (rdr.ReadToNextSibling("List"));
              }
            }
          }
           break;
         default:
           break;
       }
    }
 }

Notice the use of a switch block in this code snippet to handle the upgradeActionName. This value is specified in the Name attribute of the CustomUpgradeAction element in the feature manifest.

6.If we deploy our updated feature using Visual Studio, our existing version will be removed first, which will make it impossible to test our upgrade process. Instead, we’ll package our solution using Visual Studio and deploy it manually. From the Build menu, select Package.

7.To test our upgrade process quickly, we can use PowerShell to upgrade a single feature. Choose Start | SharePoint 2010 Management Shell, and then enter the following script:

  update-spsolution -identity Example 19.wsp -literalpath c:\code\example19\→
  example19\bin\debug\example19.wsp -gacdeployment

Note This command should be entered as a single line.

8.This command will upgrade the Example19 solution package to the latest version. We can confirm this by entering the following script:

$featureName="Example19_feature2"
$latestVersion=(get-spfeature|where {$_.DisplayName -eq $featureName}).Version
$web=get-spweb http://<your Server Name>/example19
$theFeature=$web.Features|Where {$_.Definition.DisplayName -eq $featureName}
$currentVersion=theFeature.Version
write-host "Current Version: $currentVersion, Latest Version: $latestVersion"

If all is well, the resultant output should be this:

Current Version: 1.0.0.0, Latest Version: 2.0.0.0

9.We can upgrade a single feature using the following script:

$web=get-spweb http://<your Server Name>/example19
$theFeature=$web.Features|Where {$_.Definition.DisplayName -eq $featureName}
$theFeature.Upgrade($false)

10.Any errors that occur as part of the upgrade process will be shown in the PowerShell window. However, we can confirm that our upgrade was successful by issuing the following command:

  write-host ($theFeature).Version

The new version number should be reported as 2.0.0.0.

http://allcomputers.us/windows_server/sharepoint-2010---packaging-and-deployment-model---features-(part-3)---upgrading-features.aspx

3
  • this answer has nothing to do with the feature upgrade toolkit that I am talking about from Chris O Brien. The solution has been already installed, but there is a feature that changed from V1 to V2 and I want to upgrade it using powershell(from the toolkit developed by Chris), see link, I wonder if that is possible or not, because his example only shows the command that will upgrade ALL farm features at once. Aug 30, 2013 at 12:39
  • I think this last part is all I need: $web=get-spweb http://<your Server Name>/example19 $theFeature=$web.Features|Where {$_.Definition.DisplayName -eq $featureName} $theFeature.Upgrade($false) Aug 30, 2013 at 12:46
  • At point 9 I get <nativehr>0x80070002</nativehr><nativestack></nativestack>
    – Emaborsa
    Sep 26, 2022 at 19:30
3

You can call Upgrade method on desired SPFeature object. Depending on the feature scope you might need different code to fetch that SPFeature object.

Example for site scoped feature:

$id = new-object System.Guid("<feature_id_here>")
$site = Get-SPSite "<site_url_here>"
$feature = $site.Features[$id]
$feature.Upgrade($false)
1

Although Ali Jafer gave a very comprehensive answer but in my case these commands using Feature Name did not work. I used Feature ID and here is how my powershell commands look like.

Update-SPSolution –Identity "Example.wsp" –LiteralPath "E:\Visual Studio 2015\Projects\Example.wsp" –GacDeployment  


$featureId = new-object System.Guid("FeatureID here")  
$latestVersion=(get-spfeature|where {$_.Id -eq $featureId}).Version 
$web=get-spweb "URL"  
$theFeature=$web.Features|Where {$_.Definition.Id -eq $featureId} 
$currentVersion=$theFeature.Version 
write-host "Current Version: $currentVersion, Latest Version: $latestVersion" 



$web=get-spweb "URL"  
$theFeature=$web.Features|Where {$_.Definition.Id -eq $featureId} 
$theFeature.Upgrade($false) 
0

Using PowerShell to execute SPFeature.Upgrade() after running the Update-SPSolution cmdlet, it is necessary to close the session and open a new instance of PowerShell (PowerGUI etc) before executing SPFeature.Upgrade(). Failure to do so will cause the feature to appear as if no upgrade is available. I experienced this when upgrading a Web-scope feature (so it may be limited to Web-scope and not an issue for Site-scope) Found others with the same experience here and here

1
  • I had this problem with Site scoped features. Thanks for the tip Nov 10, 2014 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.