SharePoint功能升级错误捕获

时间:2016-05-12 22:07:50

标签: c# asp.net powershell sharepoint

我有一个PowerShell脚本而不是调用SPFeature.Upgrade(false)方法。然后在c#中的FeatureUpgrading方法内部,我抛出异常来模拟升级失败。但是,升级仍会成功执行,并且功能版本会升级。如何在发生异常时阻止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#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脚本

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

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我不得不再次在c#catch块中抛出错误,以便升级停止。

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