Convert Classic pages to Modern pages using PNP Powershell Script

Introduction-

Classic SharePoint sites have classic pages like wiki pages, web part pages, blog pages or publishing pages which cannot be used in modern user interfaces. But a classic site can host modern pages which enables a great end user experience.

In this blog we are going to discuss about how to transform classic pages to modern page using PNP PowerShell.

Powershell code-

In this script, We have elaborated the steps for converting a wiki page or a webpart page to Modern page,

1- First give the site url and then set the credentials.

2- Get all the pages from Site page library. We have used ‘-PageSize‘parameter to ensure the query works when there are more than 5000 items in the list.

3- We have used ‘ConvertTo-PnPClientSidePage‘ for converting the classic page to modern page. I have elaborated the properties which we used for modernization.

TakeSourcePageName :

            If we set the ‘sourcePageName‘ to $false , It will create a new modern page with name  ‘Migrated_<pagename>.aspx‘. If we set it to $true, It will rename the old classic page name as ‘Previous_<pagename>.aspx‘ and the new modern page will be created as ‘<pagename>.aspx’.

 –Overwrite:

            If you run the modernization multiple times you need to overwrites the target page.

LogVerbose:

             If you want more details logged add this switch to enable verbose logging

KeepPageCreationModificationInformation:

           It keeps the same author/editor/created/modified information in the modern page as the original page.

CopyPageMetadata:

            It use to copy the metadata of the original page to creat modern page.

Code snippet-

 try

 {

 $SiteUrl=Read-Host “SiteUrl”

 $sourcePageName = $false

 $folderLocation = $(Get-Location)

    Connect-PnPOnline -Url $SiteUrl -UseWebLogin

    sleep 5

    try{

       Write-Host “Enabling the modern page feature.” -ForegroundColor Green

       Enable-PnPFeature -Identity “B6917CB1-93A0-4B97-A84D-7CF49975D4EC” -Scope Web -Force

    }catch {}

    $pages = Get-PnPListItem -List sitepages -PageSize 500

    Write-Host “Starting the modernization.” -ForegroundColor Green

    Foreach($page in $pages)

    {

        $pageName = $page.FieldValues[“FileLeafRef”]

        if ($page.FieldValues[“ClientSideApplicationId”] -eq “b6917cb1-93a0-4b97-a84d-7cf49975d4ec” )

        {

            Write-Host “”$page.FieldValues[“FileLeafRef”]” is a modern page” -ForegroundColor Yellow

        }

        else

        {

            Write-Host “Converting $($pageName) to modern page” -ForegroundColor Cyan

            ConvertTo-PnPClientSidePage -Identity $page.FieldValues[“ID”] `

                                        -Overwrite `

                                        -TakeSourcePageName:$sourcePageName `

                                        -LogType File `

                                        -LogFolder $folderLocation `

                                        -LogSkipFlush `

                                        -KeepPageCreationModificationInformation `

                                        -CopyPageMetadata

        }

    }

    Write-Host “The conversion log file.” -ForegroundColor Green

    Save-PnPClientSidePageConversionLog

    Write-Host “Successfully converted to modern pages.” -ForegroundColor Green

    Disconnect-PnPOnline

      }

      catch {

        Write-Host -ForegroundColor Red ‘Error ‘, ‘:’ $Error[0].ToString();

         sleep 10

      }

Classic site page

Run the script,Give the SiteUrl.

Give the credentials.

-While running the script it will show message as below.

After Successfully Running the script the modern pages will be like the below screenshots.

Classic page

Modern page

Key Words:

-Convert SharePoint Classic pages to Modern pages using PNP PowerShell Script
-Modernizing the Classic pages using PNP PowerShell Script
-Transform the classic pages to modern pages using PowerShell
-convert wiki page or web-part page to modern page using PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *