I'm using SharePoint 2013 (Office 365), and I'm writing scripts in PowerShell using the CSOM to deploy various bits to Office 365.
I'm trying to add a web part to a page using PowerShell + CSOM.
I have this process working for publishing pages – that works fine.
I'm trying to add web parts to non publishing pages, such as 'NewForm.aspx' within a list. I understand that we can't checkout / checkin these pages, and webparts are added through design mode – however I can't get my script to add a simple web part to these pages (Content Editor Web Part).
My code:
$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()
Any ideas?
Many thanks.
Best Answer
Ok, I got it working using the following: