首页 文章

SharePoint 2013 - 使用客户端对象模型将Web部件添加到页面

提问于
浏览
2

我正在使用SharePoint 2013(Office 365),我正在使用CSOM在PowerShell中编写脚本以将各种位部署到Office 365 .

我正在尝试使用PowerShell CSOM将Web部件添加到页面 .

我有这个过程用于发布页面 - 工作正常 .

我正在尝试将Web部件添加到非发布页面,例如列表中的“NewForm.aspx” . 我知道我们无法签出/签入这些页面,并且webparts是通过设计模式添加的 - 但是我无法让我的脚本向这些页面添加简单的Web部件(内容编辑器Web部件) .

我的代码:

$wpxml = '<?xml version="1.0" encoding="utf-8"?>
<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>Form View JS</Title>
  <FrameType>None</FrameType>
  <Description>Allows authors to enter rich text content.</Description>
  <IsIncluded>true</IsIncluded>
  <ZoneID>Main</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>
  <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor">/sites/forms/Style Library/en-us/JS/mobileform.js</ContentLink>
  <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
  <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
</WebPart>'


$serverRelativeUrl = '/sites/forms/Lists/abc/NewForm.aspx'
$file = $ctx.get_web().getFileByServerRelativeUrl($serverRelativeUrl)
$ctx.Load($file)
$ctx.ExecuteQuery()

#$file.CheckOut()
$ctx.ExecuteQuery()
$limitedWebPartManager = $file.getLimitedWebPartManager("User")
$wpd = $limitedWebPartManager.ImportWebPart($wpxml)
$limitedWebPartManager.AddWebPart($wpd.WebPart, "Main", "0")
#$file.CheckIn("Script web part added via PS", "MajorCheckIn")
$ctx.ExecuteQuery()

有任何想法吗?

非常感谢 .

2 回答

  • 0

    好的,我使用以下方法工作:

    $serverRelativeUrl = '/sites/forms/Lists/abc/NewForm.aspx'
    
    $WPManager = $web.GetFileByServerRelativeUrl($serverRelativeUrl).GetLimitedWebPartManager("Shared")
    $wpd = $WPManager.ImportWebPart($wpxml)
    
    $file = $web.GetFileByServerRelativeUrl($serverRelativeUrl).GetLimitedWebPartManager("Shared").AddWebPart($wpd.WebPart, "Main", "0")
    $ctx.Load($file)
    $ctx.ExecuteQuery()
    
  • 0

    如果要直接从XML变量添加,请注意以下行:

    导入webpart $ wp = $ webpartManager.ImportWebPart($ WebPartXml.OuterXml)

    将OuterXml属性输出为字符串输出所有xml .

    您可以在此处查看更多信息:http://josharepoint.com/2016/02/16/using-powershell-to-add-webpart-to-sharepoint-page-via-csom-in-office-365/

相关问题