SharePoint Online: Convert Classic Page to Modern Page using PowerShell
Requirement: Convert classic page to modern in SharePoint Online.
SharePoint Online: Convert Classic Page to Modern using PowerShell
The Modern pages are the new standard for SharePoint and offer a number of advantages over classic pages, including improved performance, an intuitive user interface, and support for responsive design. In this blog post, we’ll provide a step-by-step guide on how to convert a classic page to a modern page in SharePoint Online.
We can convert a classic page (wiki or web part page) into a modern page using the ConvertTo-PnPPage cmdlet.
PowerShell to Convert a Classic Page to Modern Page
To transform a classic page to a modern page, use this PowerShell script. Make sure the “Site Pages” feature is activated in the classic sites before using this script.
#Set Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ClassicPageName = "home.aspx"
#Connect to Site
Connect-PnPOnline $SiteURL -Interactive
#Convert Classic page to Modern page
ConvertTo-PnPPage -Identity $ClassicPageName -AddPageAcceptBanner
This will convert the classic page to a modern page, which will have a different layout and functionality than the classic version. The converted page will be named “Migrated_<pagename>.aspx”. Here is the converted page:
PowerShell to Convert All Classic Pages to Modern Page
Let’s convert all pages in the “Site pages” library to modern using PnP PowerShell.
#Set Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
#Connect to Site
Connect-PnPOnline $SiteURL -Credentials (Get-Credential) #-Interactive
#Get All Pages from "Site Pages" Library
$Pages = Get-PnPListItem -List SitePages -PageSize 500
Try {
ForEach($Page in $Pages)
{
#Get the page name
$PageName = $Page.FieldValues.FileLeafRef
Write-host "Converting Page:"$PageName
#Check if the page is classic
If($Page.FieldValues["ClientSideApplicationId"] -eq "b6917cb1-93a0-4b97-a84d-7cf49975d4ec")
{
Write-host "`tPage is already Modern:"$PageName -f Yellow
}
Else
{
#Conver the classic page to modern page
ConvertTo-PnPPage -Identity $PageName -Overwrite -TakeSourcePageName -AddPageAcceptBanner
Write-host "`tPage Converted to Modern!" -f Green
}
}
}
Catch {
write-host -f Red "Error Converting Clasic Page to Modern!" $_.Exception.Message
}
This PnP PowerShell script converts classic pages, renames the old files prefixed with “Previous_<pagename>,” and saves the converted modern pages as the source page so that it doesn’t end up with any broken links. It is important to note that the conversion process will change the layout and functionality of the page, so it is important to review the page and make any necessary adjustments after the conversion.
Please note, if your site is created with the classic site template, You have to make sure the modern site pages feature is activated. To create a modern page on a classic SharePoint Online site, refer: SharePoint Online: How to Create a Modern Page in Classic Site?
My script is attempting to convert all the classic pages within the pages library that is outside the Site Pages library. I am using the following command:
ConvertTo-PnPPage -Identity $PageName -CopyPageMetadata -Overwrite -TakeSourcePageName -AddPageAcceptBanner -Library “https://contoso.sharepoint.com/sites/intranet-Contoso/subsite”
However, it says:
“Converting page: TestPage.aspx”
Error Converting Clasic Page to Modern! Page ‘TestPage.aspx’ does not exist
I have tried on both Powershell 5 as well as Powershell 7.
Any suggestions?
How about a try / catch for when a page hangs and there’s a non-terminating error, so a timeout value of say 3 minutes and then continue noting page did not convert successfully due to timeout.
Sure! Updated the script.
Getting the same page does not exist error with or without the -CopyPageMetadata parameter
If your pages are outside the “Site Pages” library, use: -Library ‘Library-url'” or -Folder ‘Folder-URL’ parameters.
I get a lot of
ConvertTo-PnPPage : Page does not exist, any ideas?
Can you try with -CopyPageMetadata parameter?