2016-05-12 74 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; 
    }