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
We can convert a classic page (wiki or web part page) into a modern page using ConvertTo-PnPClientSidePage cmdlet.
Convert classic page to modern in SharePoint Online

PowerShell to Convert a Classic Page to Modern Page
#Set Parameters
$ClassicPageName = "home.aspx"

#Connect to Site
Connect-PnPOnline $SiteURL -UseWebLogin

#Convert Classic page to Modern page
ConvertTo-PnPClientSidePage -Identity $ClassicPageName -AddPageAcceptBanner

The converted page will be named as "Migrated_<pagename>.aspx". Here is the converted page:
sharepoint online convert classic page to modern

PowerShell to Convert All Classic Pages to Modern Page
Let's convert all pages in "Site pages" library to modern using PnP PowerShell.
#Set Parameters

#Connect to Site
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)  #-UseWebLogin

#Get All Pages from "Site Pager" Library
$Pages = Get-PnPListItem -List SitePages -PageSize 500

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 
        #Conver the classic page to modern page
        ConvertTo-PnPClientSidePage -Identity $PageName –Overwrite -TakeSourcePageName -AddPageAcceptBanner
        Write-host "`tPage Converted to Modern!" -f Green     
This PnP PowerShell script converts classic pages and renames the old files prefixed with "Previous_<pagename>" and saves the converted modern pages as source page

Please note, if your site is created with classic site template, You have to make sure modern site pages feature is activated. To create modern page in classic SharePoint Online site, refer: SharePoint Online: How to Create Modern Page in Classic Site?

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.