powershell - 如何使用 Power Shell 在共享点页面中添加 Web 部件?
问题描述
我在通过在线共享点在空白页面中添加 Web 部件时遇到问题
没有出现错误,但页面中没有更新
页面测试是在线共享点的空白页面,并且也签入
多次搜索添加 Web 部件后,我有以下代码
function AddWebPartToPage ($ctx, $sitesURL) {
$pageRelativeUrl = "/Pages/test.aspx"
$wpZoneID = "Left"
$wpZoneOrder= 0
$WebPartXml = [xml] "
<WebPart xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.microsoft.com/WebPart/v2'>
<Title>User Properties</Title>
<FrameType>None</FrameType>
<Description>Allows authors to enter rich text content.</Description>
<IsIncluded>true</IsIncluded>
<ZoneID>QuickLinks</ZoneID>
<PartOrder>0</PartOrder>
<FrameState>Normal</FrameState>
<Height />
<Width />
<AllowRemove>true</AllowRemove>
<AllowZoneChange>true</AllowZoneChange>
<AllowMinimize>true</AllowMinimize>
<AllowConnect>true</AllowConnect>
<AllowEdit>true</AllowEdit>
<AllowHide>true</AllowHide>
<IsVisible>true</IsVisible>
<DetailLink />
<HelpLink />
<HelpMode>Modeless</HelpMode>
<Dir>Default</Dir>
<PartImageSmall />
<MissingAssembly>Cannot import this Web Part.</MissingAssembly>
<PartImageLarge>/_layouts/15/images/mscontl.gif</PartImageLarge>
<IsIncludedFilter />
<Assembly>Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
<Content xmlns='http://schemas.microsoft.com/WebPart/v2/ContentEditor'><div>test 2019</div></Content>
<PartStorage xmlns='http://schemas.microsoft.com/WebPart/v2/ContentEditor' />
</WebPart>"
try{
Write-Host "Starting the Process to add the User WebPart to the Home Page" -ForegroundColor Yellow
#Adding the reference to the client libraries. Here I'm executing this for a SharePoint Server (and I'm referencing it from the SharePoint ISAPI directory,
#but we could execute it from wherever we want, only need to copy the dlls and reference the path from here
Add-Type -Path '*************\Microsoft.SharePoint.Client.dll'
Add-Type -Path '****************\Microsoft.SharePoint.Client.Runtime.dll'
Write-Host "Getting the page with the webpart we are going to modify" -ForegroundColor Green
#Using the params, build the page url
$pageUrl = $sitesURL + $pageRelativeUrl
Write-Host "Getting the page with the webpart we are going to modify: " $pageUrl -ForegroundColor Green
#Getting the page using the GetFileByServerRelativeURL and do the Checkout
#After that, we need to call the executeQuery to do the actions in the site
$page = $ctx.Web.GetFileByServerRelativeUrl($pageUrl);
$page.CheckOut()
$ctx.ExecuteQuery()
try{
#Get the webpart manager from the page, to handle the webparts
Write-Host "The page is checkout" -ForegroundColor Green
$webpartManager = $page.GetLimitedWebPartManager([Microsoft.Sharepoint.Client.WebParts.PersonalizationScope]::Shared);
Write-Host $WebPartXml.OuterXml
#Load and execute the query to get the data in the webparts
Write-Host "Getting the webparts from the page" -ForegroundColor Green
$ctx.Load($webpartManager);
$ctx.ExecuteQuery();
#Import the webpart
$wp = $webpartManager.ImportWebPart($WebPartXml.OuterXml)
#Add the webpart to the page
Write-Host "Add the webpart to the Page" -ForegroundColor Green
$webPartToAdd = $webpartManager.AddWebPart($wp.WebPart, $wpZoneID, $wpZoneOrder)
$ctx.Load($webPartToAdd);
$ctx.ExecuteQuery()
}
catch{
Write-Host "Errors found:`n$_" -ForegroundColor Red
}
finally{
#CheckIn and Publish the Page
Write-Host "Checkin and Publish the Page" -ForegroundColor Green
$page.CheckIn("Add the User Profile WebPart", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
$page.Publish("Add the User Profile WebPart")
$ctx.ExecuteQuery()
Write-Host "The webpart has been added" -ForegroundColor Yellow
}
}
catch{
Write-Host "Errors found:`n$_" -ForegroundColor Red
}
}
$tenantAdmin = "****@***.com"
$tenantAdminPassword = "****************"
$secureAdminPassword = $(convertto-securestring $tenantAdminPassword -asplaintext -force)
$siteURL = "https://"*****************.com/sites/****************/*********";
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($tenantAdmin, $secureAdminPassword)
$ctx.Credentials = $credentials
######################################
# Set Add WebPart to Page Parameters #
######################################
$relUrl = "/sites/****************/*********"
AddWebPartToPage $ctx $relUrl
从上面的代码预期该页面将包含 div
2019 年测试
但它没有出现
解决方案
将 webpart 添加到 Sharepoint 在线页面的最简单方法是使用 PnP Powershell 您可以使用以下命令将 webpart 添加到页面
Add-PnPWebPartToWebPartPage -ServerRelativePageUrl $serverRelativeUrlOfPage -Xml $webPartXML -ZoneId $zoneId -ZoneIndex $zoneIndex
推荐阅读
- java - 在休眠状态下,当有 EAGER JOIN 时,StatelessSession 是否会阻止过滤掉重复项
- python - 计数视图有时有效,有时不计数
- single-sign-on - ADFS 问题 - 自动登录不起作用
- git - RPC 失败;HTTP 502 curl 22 The requested URL returned error: 502 Bad Gateway?
- javascript - Firebase OnCreate() 触发器有时只会触发
- angular - Mat-option select 从 Map 中设置默认选择的选项
- amazon-web-services - AWS Glue DynamicDataFrame 报告 RDS 数据库的零记录
- react-native - 如何在 React Native 上正确分层组件
- c# - 在 Windows 10 IoT Core 上,基于 HT16K33 的十四段控制并不总是显示给定位掩码的正确字符
- javascript - 用图像填充数百个 SVG 路径会导致严重的性能问题