這是我如何做到這一點:
之前部署我更新的功能,我打開Visual Studio中的功能和更改版本屬性(當我創建新的功能,我的版本設置爲1.0.0.0)。接下來,我單擊Manifest按鈕,展開編輯選項部分,然後輸入UpgradeActions數據。
下面是一個略加修改的版本升級後的功能:
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/">
<UpgradeActions>
<VersionRange BeginVersion="1.0.0.0" EndVersion="1.1.0.0">
<CustomUpgradeAction Name="UpgradeTo1v1" />
</VersionRange>
<VersionRange BeginVersion="1.1.0.0" EndVersion="1.3.0.0">
<CustomUpgradeAction Name="UpgradeTo1v3" />
</VersionRange>
<VersionRange BeginVersion="1.3.0.0" EndVersion="1.4.0.0">
<CustomUpgradeAction Name="UpgradeTo1v4">
<Parameters>
<Parameter Name="OptionalParameter">Values go here</Parameter>
</Parameters>
</CustomUpgradeAction>
</VersionRange>
</UpgradeActions>
</Feature>
我再添加或修改我的功能接收器的FeatureUpgrading方法:
[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
public override void FeatureUpgrading(
SPFeatureReceiverProperties properties,
string upgradeActionName,
System.Collections.Generic.IDictionary<string, string> parameters)
{
SPSite site = properties.Feature.Parent as SPSite;
SPWeb web = site.RootWeb;
switch (upgradeActionName)
{
case "UpgradeTo1v1":
UpgradeTo1v1(site, properties, parameters);
break;
case "UpgradeTo1v3":
UpgradeTo1v3(site, properties, parameters);
break;
case "UpgradeTo1v4":
UpgradeTo1v4(site, properties, parameters);
break;
}
}
我的私人升級方法作任何以編程方式進行更改,這些更新後的解決方案軟件包部署時不會自動實現。我部署使用下面的PowerShell腳本解決方案包:
Write-Host "Upgrading solution: $SolutionPackageName" -NoNewline
Update-SPSolution -Identity $SolutionPackageName -LiteralPath $SolutionPackagePath -GACDeployment -Confirm:$false -Force
while ($Solution.JobExists)
{
Start-Sleep -s 2
}
Write-Host " - Done."
if ($UpdateFeatureGuids -ne $null)
{
foreach ($site in Get-SPSite -Limit All)
{
$UpdateFeatureGuids | % { $site.QueryFeatures([Guid]$_, [bool]$true) } | % {
$definitionId = $_.DefinitionId
$parentUrl = $_.Parent.Url
Write-Host "Upgrading feature: $definitionId $parentUrl"
$_.Upgrade([bool]$false) | % {
if ($_ -ne $null)
{
Format-List -InputObject $_ -Property Message, InnerException -Force
}
}
Write-Host "Upgrading feature: $definitionId $parentUrl - Done."
}
$site.Dispose()
}
}
我真的希望Update-SPSolution
了,讓你升級所有的活動功能的標誌。我還沒有遇到需要更新功能但不希望程序升級適用的情況。因此,剩下的PowerShell代碼通過遍歷所有網站集,爲我找到了需要根據預定義列表進行升級的所有功能,然後在顯示發生的任何錯誤的同時升級每個網站上的功能。
請注意,Update-SPSolution
只刷新現有的文件。如果您已將新文件添加到解決方案包中,則它們將被忽略並不會被部署。我希望某個功能的版本歷史記錄和升級能力不會因收回/部署而無效,但我從未嘗試過。通常,如果我需要添加新文件,我將創建一個新的解決方案包。