[BUG] - Add-PnPPageImageWebPart inside a loop corrupts pages
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 904
- Forks
- 407
- Avg merge
- 9h 16m
- Merged PRs (30d)
- 10
Description
Reporting an Issue or Missing Feature
Add-PnPPageImageWebPart causes pages to corrupt (revert to base sharepoint page with webparts and then users are unable to edit them in the UI) if it is part of a loop that adds components to the page.
This does not happen if you run it out of the context of the loop and it does not happen if you re-fetch the page context before running the line.
Expected behavior
Add-PnPPageImageWebPart should just add an image to the page just like Add-PnPPageTextPart or Add-PnPPageWebPart
Actual behavior
After running the Add-PnPPageImageWebPart line in the script, the page reverts to some built-in sharepoint home page and loses all modifications done to it. sharepoint does not let users edit the page saying something went wrong and to try again.
Steps to reproduce behavior
$existingpage = get-pnppage -Identity $targetPageName -Connection $webConnection
$global:pageContentArray = @()
$global:pageContentArray += @{
"Type" = "Section"
"Layout" = [PnP.Core.Model.SharePoint.CanvasSectionTemplate]::TwoColumnLeft
"Order" = 1
}
$global:pageContentArray += @{
"Type" = "Text"
"HTML" = "
sample
""Section" = 1
"Column" = 1
}
$global:pageContentArray += @{
"Type" = "Text"
"HTML" = "
another sample
""Section" = 1
"Column" = 1
}
$global:pageContentArray += @{
"Type" = "Image"
"ImageUrl" = "/sites/mysite/mylibrary/image.jpg"
"Section" = 1
"Column" = 1
}
foreach ($contentItem in $global:pageContentArray) {
try {
switch ($contentItem.Type) {
"Section" {
$null = Add-PnPPageSection -Page $existingPage -SectionTemplate $contentItem.Layout -Connection $webconnection -Order $contentItem.Order
}
"Image" {
$null = Add-PnPPageImageWebPart -Page $existingPage -ImageUrl $contentItem.ImageUrl -Connection $webConnection # -Section $contentItem.Section -Column $contentItem.Column
}
"Text" {
$content = $contentItem.HTML
$null = Add-PnPPageTextPart -Connection $webConnection -Page $existingPage -Text $content -Section $contentItem.Section -column $contentItem.Column
}
}
}
catch {
write-host "Failed to add page component"
Get-PnPException
}
}
IMPORTANT NOTE!
this issue is resolved if you put
$existingpage = get-pnppage -Identity $targetPageName -Connection $webConnection
before the Add-PnPPageImageWebPart line. This slows down the script as there is now an extra call to re-fetch the page.
What is the version of the Cmdlet module you are running?
2.99.54
Which operating system/environment are you running PnP PowerShell on?
- [ X] Windows
- Linux
- MacOS
- Azure Cloud Shell
- Azure Functions
- Other : please specify
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the script using Add-PnPPageSection, Add-PnPPageTextPart, and Add-PnPPageImageWebPart in PnP PowerShell 2.99.54, then compare behavior with and without refetching $existingPage. Done means adding the image inside the loop no longer corrupts the page, removes its modifications, or prevents editing in SharePoint.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- powershell
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100