简体   繁体   中英

SharePoint Feature upgrade error catching

I have a PowerShell script than calls a SPFeature.Upgrade(false) method. then inside the FeatureUpgrading method in c#, i am throwing an exception to simulate a upgrade fail. However, the upgrade still executes successfully and the feature version gets upgraded. How do i prevent a SharePoint feature from upgrading when an exception happens? here's what i have:

// feature.template.template.xml

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"         Version="$SharePoint.Project.AssemblyVersion$">
 <UpgradeActions>
  <VersionRange BeginVersion="0.0.0.0">
   <CustomUpgradeAction Name="AllUpgrades" />
   <ApplyElementManifests>
     <ElementManifest Location="test\Elements.xml" />
   </ApplyElementManifests>
  </VersionRange>
 </UpgradeActions>
</Feature>

// c# Feature Upgrading

 public override void FeatureUpgrading(SPFeatureReceiverProperties p_properties, string p_upgradeActionName, System.Collections.Generic.IDictionary<string, string> p_parameters)
    {
        try
        {
            SPWeb parentWeb = p_properties.Feature.Parent as SPWeb;
            using (SPSite site = new SPSite(parentWeb.Url, parentWeb.Site.SystemAccount.UserToken))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    switch (p_upgradeActionName)
                    {
                      case "AllUpgrades":
                        throw new Exception("Simulate failure");
                    }
                }
            }

        }
        catch (Exception p_ex)
        {
           // Log message to SharePoint logs
        }
    }

// Power Shell script

Add-PSSnapin Microsoft.SharePoint.PowerShell 
$web = Get-SPWeb "http://myURL"
$featureName = "featureName"
feature = $web.Features|Where {$_.Definition.DisplayName -eq $featureName}

try
 {
   // I would expect this call to fail and return exception from c#, but it doesn't. instead feature upgrade just fine
   $feature.Upgrade($false)
 }
catch [Exception] 
{
  Write-Host ($_.Exception.Message)
}

Any help would be greatly appreciated!

I had to throw the error again in the c# catch block for the upgrade to halt.

catch (Exception p_ex)
    {
       // Log message to SharePoint logs
       Throw p_ex;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM