WARNING: Microsoft supports Site Pages libraries created through either calling EnsureClientRenderedSitePagesLibrary(), EnsureSitePagesLibrary(), or by activating the Site Pages feature (which does the first one internally). These methods all create the library correctly, with the right content types, and with the web-relative URL of /SitePages/.
The below article is just for your info only!
SitePages has the following FeatureID: b6917cb1-93a0-4b97-a84d-7cf49975d4ec
So on with PnP:
In order to create the new Page library we have to take into consideration that the Page Library template is 119
So, here goes:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#Set Parameters $SiteURL = "https://tenant.sharepoint.com/sites/COMTESTAPR2022" $LibraryName = "andreitest1" #Connect to PnP Online Connect-PnPOnline -Url $SiteURL -Interactive $Ctx = Get-PnPContext #create document library in sharepoint online powershell $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation $ListInfo.Title = $LibraryName $ListInfo.TemplateType = 119 #site pages/Page Library template $List = $Ctx.Web.Lists.Add($ListInfo) $List.Update() $Ctx.ExecuteQuery() |
and here is the result:
Now we see that compared to the original Site Pages library, we are missing a content type: named “Site Page”
So let’s add the Site Page content type:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#Config Variables $SiteURL = "https://tenant.sharepoint.com/sites/COMTESTAPR2022/" $ContentTypeName="Site Page" $ListName="andreitest1" #Connect to PnP Online Connect-PnPOnline -Url $SiteURL -Interactive #Get the content type $ContentType = Get-PnPContentType -Identity $ContentTypeName If($ContentType) { #Add Content Type to Library Add-PnPContentTypeToList -List $ListName -ContentType $ContentType } else { Write-host -f Yellow "Could Not Find Content Type:"$ContentTypeName } |
you’ll see the content type added:
and we need to remove the “Wiki Page” content type:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#Config Variables $SiteURL = "https://tenant.sharepoint.com/sites/COMTESTAPR2022/" $ListName ="andreitest1" $ContentTypeName ="Wiki Page" #Connect to PnP Online Connect-PnPOnline -Url $SiteURL -Interactive $Context = Get-PnPContext #Get the content type from list $ListContentType = Get-PnPContentType -list $ListName -Identity $ContentTypeName #Delete the content type from List $ListContentType.DeleteObject() $Context.ExecuteQuery() |
and here are the final results:
OK, now that we have the library created, we need to copy the items from the old site pages library to our new library:
|
1 2 3 4 5 6 7 8 9 10 |
Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/COMTESTAPR2022" -Interactive $allItems = Get-PnPListItem -List "Site Pages" -FolderServerRelativeUrl "/sites/COMTESTAPR2022/SitePages" foreach ($item in $allItems) { if ($item.FileSystemObjectType -eq "File") { Write-Host "Copying file: $($item.FieldValues.FileLeafRef)" -ForegroundColor green Copy-PnPFile -SourceUrl "$($item.FieldValues.FileRef)" -TargetUrl ("/sites/COMTESTAPR2022/andreitest1/"+$item.FieldValues.FileLeafRef) -Overwrite } } |
and the results:
The logic is even simpler for a single file:
|
1 2 3 4 5 6 7 8 9 10 |
#Config Variables $SiteURL = "https://tenant.sharepoint.com/sites/COMTESTAPR2022" $SourceURL= "/sites/COMTESTAPR2022/DOC1/Anexa8.pdf" $TargetURL = "/sites/COMTESTAPR2022/DOC2/Anexa8.pdf" #Connect to PnP Online Connect-PnPOnline -Url $SiteURL -Interactive #copy files in sharepoint online using powershell copy-PnPFile -SourceUrl $SourceURL -TargetUrl $TargetURL |
Now to rename the newly created Page Library we can use:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Set Parameters $SiteURL = "https://tenant.sharepoint.com/sites/COMTESTAPR2022" $ListName = "andreitest1" $NewListURL = "PNPList" # Connect to PNP Online Connect-PnPOnline -Url $SiteURL -Interactive # Fetch the list by it's GUID $list = Get-PnPList $ListName # Rename the list relative url to the url $list.Rootfolder.MoveTo($NewListURL) Invoke-PnPQuery |
and the results:
To just change the list’s name you can use the following script: (bear in mind that the list’s URL won’t change)
|
1 2 3 4 5 6 7 8 9 10 |
#Config Variables $SiteURL = "https://tenant.sharepoint.com/sites/COMTESTAPR2022" $oldlistname = "PNPList" $newlistname = "andreitest2" #Connect to PnP Online Connect-PnPOnline -Url $SiteURL -Interactive #Rename "Projects" List to "Projects Archive" Set-PnPList -Identity $oldlistname -Title $newlistname |
To remove the old or any list, you can always use:
|
1 |
Remove-PnPList "andreitest2" |
Enjoy!








