Powershell SharePoint 2013-使用客户端对象模型将web部件添加到页面
我正在使用SharePoint 2013(Office 365),我正在PowerShell中编写脚本,使用CSOM将各种BIT部署到Office 365 我正在尝试使用PowerShell+CSOM将web部件添加到页面 我有一个发布页面的过程,效果很好 我正在尝试将web部件添加到非发布页面,例如列表中的“NewForm.aspx”。我知道我们无法签出/签入这些页面,web部件是通过设计模式添加的-但是我无法让脚本向这些页面添加简单的web部件(Content Editor web部件) 我的代码:Powershell SharePoint 2013-使用客户端对象模型将web部件添加到页面,powershell,sharepoint,Powershell,Sharepoint,我正在使用SharePoint 2013(Office 365),我正在PowerShell中编写脚本,使用CSOM将各种BIT部署到Office 365 我正在尝试使用PowerShell+CSOM将web部件添加到页面 我有一个发布页面的过程,效果很好 我正在尝试将web部件添加到非发布页面,例如列表中的“NewForm.aspx”。我知道我们无法签出/签入这些页面,web部件是通过设计模式添加的-但是我无法让脚本向这些页面添加简单的web部件(Content Editor 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()
$wpxml='1!'
表单视图JS
没有一个
允许作者输入富文本内容。
真的
主要
0
正常的
真的
真的
真的
真的
真的
真的
真的
无模式
违约
无法导入此Web部件。
/_布局/15/images/mscontl.gif
Microsoft.SharePoint,版本=16.0.0.0,区域性=中性,PublicKeyToken=71e9bce111e9429c
Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
/站点/表单/样式库/en-us/JS/mobileform.JS
'
$serverRelativeUrl='/sites/forms/list/abc/NewForm.aspx'
$file=$ctx.get_web().getFileByServerRelativeUrl($serverRelativeUrl)
$ctx.Load($file)
$ctx.ExecuteQuery()
#$file.CheckOut()
$ctx.ExecuteQuery()
$limitedWebPartManager=$file.getLimitedWebPartManager(“用户”)
$wpd=$limitedWebPartManager.ImportWebPart($wpxml)
$limitedWebPartManager.AddWebPart($wpd.WebPart,“主”、“0”)
#$file.CheckIn(“通过PS添加的脚本web部件”,“MajorCheckIn”)
$ctx.ExecuteQuery()
有什么想法吗
非常感谢。好的,我使用以下方法使它工作:
$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()
如果要直接从XML变量添加,请注意这一行: 导入Web部件 $wp=$webpartManager.ImportWebPart($WebPartXml.OuterXml) 放置OuterXml属性以将所有xml作为字符串输出
您可以在此处看到更多信息:为什么它不工作?你有错误吗?没有错误,只是没有添加web部件。当我以发布页面为目标时,这个过程非常有效。