我有以下代码添加了一个webpart,但我需要一些方法来删除所有webparts之前运行。有人知道怎么做吗?我尝试了多种方法,但对PowerShell和sharepoint来说是全新的,并且不知道我在做什么。
$wpm = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
$sortedNodes = $pageXml.Module.File.AllUsersWebPart | sort { $_.WebPartZoneID, [int] $_.WebPartOrder }
write-host $sortedNodes
write-host $sortedNodes.Count
foreach ($webPartXml in $sortedNodes)
{
$zoneId = $webPartXml.WebPartZoneID
$zoneIndex = $webPartXml.WebPartOrder
$xml = $webPartXml.InnerText
$wpd = $wpm.ImportWebPart($xml)
$wpd = $wpm.AddWebPart($wpd.WebPart, $zoneId, $zoneIndex)
"Adding a web part to the [$zoneId] zone, [$zoneIndex] position..."
try
{
Submit-ExecuteQuery $context
}
catch
{
Write-Host "The following error occurred while trying to add the web part: $($_.Exception.Message)" -ForegroundColor Red
}
}
答案 0 :(得分:2)
请尝试以下方法:
$wpm = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
<# Remove All Webparts - Begin #>
foreach ($webPart in $wpm.WebParts)
{
$wpdD = New-Object Microsoft.SharePoint.Client.WebParts.WebPartDefinition
$context.Load($webPart)
$wpdD = $webPart
$wpdD.DeleteWebPart();
try
{
Submit-ExecuteQuery $context
}
catch
{
Write-Host "The following error occurred while trying to add the web part: $($_.Exception.Message)" -ForegroundColor Red
}
}
<# Remove All Webparts - End #>
$sortedNodes = $pageXml.Module.File.AllUsersWebPart | sort { $_.WebPartZoneID, [int] $_.WebPartOrder }
write-host $sortedNodes
write-host $sortedNodes.Count
foreach ($webPartXml in $sortedNodes)
{
$zoneId = $webPartXml.WebPartZoneID
$zoneIndex = $webPartXml.WebPartOrder
$xml = $webPartXml.InnerText
$wpd = $wpm.ImportWebPart($xml)
$wpd = $wpm.AddWebPart($wpd.WebPart, $zoneId, $zoneIndex)
"Adding a web part to the [$zoneId] zone, [$zoneIndex] position..."
try
{
Submit-ExecuteQuery $context
}
catch
{
Write-Host "The following error occurred while trying to add the web part: $($_.Exception.Message)" -ForegroundColor Red
}
}
&#13;