pnp / pnp/powershell

[BUG] - Add-PnPPageImageWebPart inside a loop corrupts pages

Open
#4,851 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.