2
votes

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.

2
Why is it not working? Do you get any error? - Boland
No Error, just doesn't add the web part. When I target a publishing page the process works perfectly. - dhendry

2 Answers

0
votes

Ok, I got it working using the following:

    $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
votes

If you want to add directly from XML variable take care about this line:

Import the webpart $wp = $webpartManager.ImportWebPart($WebPartXml.OuterXml)

Put the OuterXml property to output all xml as string.

You can see more information here: http://josharepoint.com/2016/02/16/using-powershell-to-add-webpart-to-sharepoint-page-via-csom-in-office-365/