pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
985 stars 665 forks source link

[BUG] "Add-PnPWebPartToWikiPage : Value cannot be null" #2921

Open vaderj opened 3 years ago

vaderj commented 3 years ago

Expected behavior

When using Add-PnPWebPartToWikiPage, and providing arguments -ServerRelativePageUrl -Xml $webPartXml -Row 1 -Column 1, I expect the command to complete successfully

Actual behavior

Please describe what you see instead. Please provide samples of HTML output or screenshots

Error received: Add-PnPWebPartToWikiPage : Value cannot be null. Parameter name: input At line:3 char:1

image

Variables: image

Steps to reproduce behavior

Please include complete code samples in-line or linked from gists

$url = "https://myTennent.sharepoint.com/sites/mySite" Connect-PnPOnline $url -UseWebLogin $items = Get-PnPListItem -List "ArticleWikiPages"

$templatePage = "/ArticleWikiPages/MAP-Sustainable-Basmati-India.aspx" $wp = (Get-PnPWebPart -ServerRelativePageUrl $templatePage)[0] $webPartXml = Get-PnPWebPartXml -ServerRelativePageUrl $templatePage -Identity $wp.Id

foreach($item in $items){ $thisUrl = $item.FieldValues.FileRef Add-PnPWebPartToWikiPage -ServerRelativePageUrl $thisUrl -XML $webPartXml -Row 1 -Column 1 }

Which version of the PnP-PowerShell Cmdlets are you using?

What is the version of the Cmdlet module you are running?

(you can retrieve this by executing Get-Module -Name sharepointpnppowershell* -ListAvailable) 3.25.2009.1

How did you install the PnP-PowerShell Cmdlets?

jackpoz commented 3 years ago

It would have been easier to reproduce the issue if the provided steps included the creating of libraries and pages required, i.e. "ArticleWikiPages" and "/ArticleWikiPages/MAP-Sustainable-Basmati-India.aspx", and the webpart itself being added

jackpoz commented 3 years ago

I created a Wiki page, added a Documents webpart, retrieved the xml and called Add-PnPWebPartToWikiPage and it worked without errors

Connect-PnPOnline $siteUrl
$sourceUrl = "/sites/PS2921/SitePages/SourceWiki.aspx"
$targetUrl = "/sites/PS2921/SitePages/TargetWiki.aspx"
$webpartXml = Get-PnPWebPartXml -ServerRelativePageUrl $sourceUrl -Identity "Documents"
Add-PnPWebPartToWikiPage -ServerRelativePageUrl $targetUrl -Row 1 -Column 1 -XML $webpartXml

image

I tried experimenting a bit and turns out I was able to get a similar error when I created a WebPart Page

image

Are you sure you created a Wiki page as target of Add-PnPWebPartToWikiPage ?

jackpoz commented 3 years ago

I created a pull request at https://github.com/pnp/PnP-Sites-Core/pull/2762 to handle the case instead of throwing an exception. It still doesn't mean that Add-PnPWebPartToWikiPage will work on any page that is not of type "Wiki Page" (content type called with that name).

You can check if the page is of type wiki by comparing the content type or by checking if the field "WikiField" exists and is not null (there are probably more ways too but these are the ones on the top of my head)

vaderj commented 3 years ago

So I was originally trying to use AddPnPWebPartToWikiPage to add to pages created by ShareGate, but I have since gone with a script which instead creates a new wikipage using Add-PnPWikiPage but I am still unable to use AddPnPWebPartToWikiPage to add a web part to the page

image

The -ServerRelativePageUrl variables - 1 is the full server relative URL, the other is relative to the site collection, without the leading '/' as described https://github.com/pnp/PnP-PowerShell/issues/848

jackpoz commented 3 years ago

Does the ArticleWikiPages library have a field called "WikiField" ? does the resulting Wiki page have the "WikiField" not null ? how did you create that library ?

vaderj commented 3 years ago

My library does NOT contain a "WikiField" property but I do have a "Wiki Content", and when I created a new Wiki Page Library its the same way

New Wiki Page library: image

I also tried adding a SLT field to my existing library called "WikiField" but the same error is still thrown

vaderj commented 3 years ago

So I found the WikiField when I called it from get-pnplistitems and when I looked at the contents of one of the wikipages fom Add-PnpWikiPage: (sorry, I lost the screenshot on a different computer - it was only a single empty div)

But when I look at a brand new page made from the GUI in a wiki library, inside the div is:

<table id="layoutsTable" style="width&#58;100%;"><tbody><tr style="vertical-align&#58;top;"><td style="width&#58;100%;"><div class="ms-rte-layoutszone-outer" style="width&#58;100%;"><div class="ms-rte-layoutszone-inner" style="min-height&#58;420px;"><p>​<br></p></div></div></td></tr></tbody></table><span id="layoutsData" style="display&#58;none;">false,false,1</span>

So I added that to each new page after add-pnpwikipage is created, and the process appears to be working now .