2016-05-12 77 views
0

我有一个PowerShell脚本比调用SPFeature.Upgrade(false)方法。然后在C#中的FeatureUpgrading方法中,我抛出一个异常来模拟升级失败。但是,升级仍然会成功执行并且功能版本会升级。如何防止发生异常时升级SharePoint功能?下面是我有:SharePoint功能升级错误捕获

// 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#功能升级

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 
     } 
    } 

//电源外壳脚本

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) 
} 

任何帮助将不胜感激!

回答

0

我不得不在c#catch块中再次抛出错误以便升级到暂停状态。

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