Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have developed an update for a SharePoint 2010 solution. This update defines some upgrades for some of the included features (the upgrade are managed via the new FeatureUpgraded receiver event).

I update the solution using the Update-SPSolution cmdlet. This will simply update the solution and won't trigger any feature upgrade for the included features, so as now I am performing the upgrade manually via powershell after the solution update.

I was wondering if there is any way to automatically upgrade all the feature that require it when the solution is Updated. For example is there any event that SharePoint triggers when the solution is updated that may be used to automate the upgrade for the included property? Or there is any interface that I can use to automate this task?

Thanks Mario

share|improve this question
    
I have reworded your question. Now it should be clearer that you were asking how to automate feature upgrade, not how upgrade works. Anyway, edit it further if you feel it would help. –  SPArchaeologist Jun 12 '12 at 12:53
    
that is correct –  gidion1983 Jun 12 '12 at 12:54
add comment

3 Answers

While it is possible to upgrade features while upgrading solution, it is a very bad practice, so only consider this as a warning of how not to do it:

Using -force parameter on Install-SPSolution will force solution to be installed and underlying features to be enabled even if they were already enabled. Forcing a feature to be enabled will also trigger feature upgrade (check the source, and you will see this is true).

EDIT: Added source from SPFeature.Activate() method

internal void Activate(SPSite siteParent, SPWeb webParent, SPFeaturePropertyCollection props, bool fForce)
    {
        if (fForce)
        {
            try
            {
                this.Upgrade(siteParent, webParent, fForce);
            }
            catch
            {
            }
        }
        ...  
    }

This is yet another reason why using -Force parameter is a very bad idea, and only should be used if the farm somehow is in a bad state. Using -Force will amongst other things make sure that any exceptions thrown during activation is swallowed (and not logged), so again: don't use it for solution deployment, use it only (carefully) to solve specific problems in your farm, such as if you removed a web application without first retracting solutions.

EDIT 2: Answer changed to fit re-worked question.

Just to be clear: Update-SPSolution only will work if you haven't added or deleted artifacts or features in your solution! If you have changed existing artifacts or manifests or changed existing code, update should work. Else you will have to disable relevant feature(s), uninstall solution, remove solution, add new solution, install solution, activate feature.

After doing that feature upgrade doesnt really make sense as you use feature upgrade, since you have re-enabled your features everywhere it was installed.

If you are only adding functionality to your existing feature, feature upgrade is an option.

This can be done either declaratively (read about available UpgradeActions here) or in code (read about the FeatureUpgrading() event and CustomUpgradeAction element here and here).

Selecting the features that needs to be upgraded can be done either in #C or better: using PowerShell.

The relevant method is called QueryFeatures() and is available on SPWebService (also has method QueryFeaturesInAllWebServices(), SPWebApplication, SPContentDatabase and SPSite. The method has several overloads.

Doing this in PowerShell is easy. In the example below we query for site (Site Collection) scoped features and upgrade them.

# find all features on all SPSites and upgrade them
Get-SPSite -Limit All | %{$_.QueryFeatures([Microsoft.SharePoint.SPFeatureScope]::Site,$false)} | %{$_.Upgrade($false)}

If you want to query for specific features you can do this like this (in this example i spread the expression over several lines to improve readability):

# Get feature
$feature = Get-SPFeature -Identity <yourfeature>
# Query if feature needs upgrade for given scope (scope must match the features scope)
$featuresForUpgrade = Get-SPSite -Limit All | %{$_.QueryFeatures($feature.Id,$true)}
# Upgrade found features
$featuresForUpgrade | %{$_.Upgrade($false)}

Note: The above code is typed in by memory, so it is not tested at all

share|improve this answer
    
thanks but Install-SPSolution is not posible. I need a reson fror Update-SPSolution. Delete Install-SPSolution -force the content from created lists? –  gidion1983 Jun 12 '12 at 11:43
    
just curious - care to reference were you saw the -force parameter forcing an upgrade? I always belived that it would just "bruteforce (re)activate" the feature, thus NOT triggering a FeatureUpgrading receiver... just like what would happen if you manually disable and then re-enable a feature that requires upgrade. –  SPArchaeologist Jun 12 '12 at 11:58
    
Sure. I have added the relevant source from Reflector –  Anders Rask Jun 12 '12 at 13:15
    
URGH. Now this wasn't expected. I suppose i have learnt something new - thank you. That said... guess I will have a look at the source when I get the time. As you said.. it seems a little -dangerous- to have this behaviour. –  SPArchaeologist Jun 12 '12 at 13:52
    
As a golden rule, never use -Force. There are ofcourse exceptions where you are stuck in a half deployment and need to get those packages working, but I have seen people use -Force as default behaviour in their deployment script and that is dangerous indeed... –  Anders Rask Jun 12 '12 at 13:56
show 1 more comment

In SharePoint 2010 the concept of Feature Upgrade has been introduced (this is on the Feature - not the solution itself). Upgrading the solution would could actually mean feature upgrading actually. If that is the case, Chris O'Brien does a pretty good job with an example explaining it here http://www.sharepointnutsandbolts.com/2010/06/feature-upgrade-part-1-fundamentals.html

Besides the regular events triggered there is also a new one for Feature_Upgrading.

share|improve this answer
    
i had implement the feature_upgrading Event. But i must ever call SPFeature.Upgrade(true) after Update-SPSolution. for this i wrote a powershell script but i think it is not the best way to update the feature. –  gidion1983 Jun 12 '12 at 11:35
add comment

Just to be clear: Update-SPSolution only will work if you haven't added or deleted artifacts > or features in your solution! If you have changed existing artifacts or manifests or changed > existing code, update should work. Else you will have to disable relevant feature(s), > uninstall solution, remove solution, add new solution, install solution, activate feature.

I'm not agree and I don't get it why it's forbidden ?! Is actually working very well. You can have a solution with Feature1, lately you can add a new Feature2. You can then do a solution upgrade and then installfeature Feature2. Everythings works perfect. I can prove it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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