Warm tip: This article is reproduced from serverfault.com, please click

powershell-使用Get-PNPWebPart获取SharePoint WebPart的列表

(powershell - Get list of SharePoint WebParts using Get-PNPWebPart)

发布于 2020-12-11 00:33:43

在SharePoint Online中,我创建了一个开箱即用的网站,并添加了两个WebPart。我想获取WebParts的列表并将其添加到其他站点。

我正在使用命令Get-PNPWebPart从网站获取WebPart。

这是我尝试过的代码。

$user = "[username]"
$password = ConvertTo-SecureString "[password]" -AsPlainText -Force

$credentials = New-Object –TypeName "System.Management.Automation.PSCredential" –ArgumentList $user, $password
 
$siteURL = "https://[tenant].sharepoint.com/Sites/[site name]"
 
Connect-PnPOnline -Url $siteURL -Credentials $credentials

$serverRelativePageUrl = "/sites/[site name]/SitePages/Home.aspx"

$webparts = Get-PNPWebPart -ServerRelativePageUrl $serverRelativePageUrl

foreach($webpart in $webparts)
{
    Write-Host "WebPart title:" $webpart.WebPart.Title
}

当我运行此程序时,我没有收到任何错误,但$webparts变量为空。

$webParts当我尝试从SharePoint网站获取WebParts列表时,为什么变量为空?

Questioner
Høgsdal
Viewed
0
Jerry_MSFT 2020-12-11 10:55:04

Get-PnPWebPart对于在现代页面中获取Web部件无效(经典页面将可用)。

相反,请使用下面的代码片段在Modern Site Home.aspx中获取Web部件:

$user = "user@tenant.onmicrosoft.com"
$password = ConvertTo-SecureString "password" -AsPlainText -Force

$credentials = New-Object –TypeName "System.Management.Automation.PSCredential" –ArgumentList $user, $password
 
$siteURL = "https://tenant.sharepoint.com/sites/JerryModernTeam"

Connect-PnPOnline -Url $siteURL -Credentials $credentials


$page=Get-PnPClientSidePage -Identity "Home.aspx"



$webParts = $page.Controls  
#if there are more than one webparts  
foreach($webpart in $webParts) {  
    Write - Host "WebPart Id "  
    $webpart.InstanceId  
    Write - Host "Title "  
    $webpart.Title  
}  

参考:

使用PnP PowerShell从现代站点页面添加,删除和获取所有Web部件