powershell
powershell copied to clipboard
Add-PnPPage: Object reference not set to an instance of an object when trying to create a new page
Category
- [x] Bug
Describe the bug
Im trying to migrate a classic site into a modern one. I already have the connected 365 group and i can use modern lists and libraries. But i have pages, like the homepage that are classic and when this page is displayed the menu is the classic one too so the user is confused. I dont want to migrate any of those pages, i tried and get other errors so i delete all of them. Now, im trying to create a new modern page using PnP. Im using command Add-PnPPage.
Steps to reproduce
DELETE THIS LINE BEFORE SUBMITTING - Provide steps to reproduce the behavior:
- Im using powershell 7 and PnP installed yesterday version 2.4.0
- Auth using
Connect-PnPOnline -Url "https://----.sharepoint.com/calidad" -Credential (Get-Credential) - Execute command
$pagename = "Welcome" - Execute command
$page = Add-PnPPage -Name $pagename -LayoutType Home - See error:
Add-PnPPage: Object reference not set to an instance of an object.
Expected behavior
The modern page to be created. A file is created instead but is corrupted.
Environment details (development & target environment)
- SDK version: 2.4.0
- OS: Windows 11
Additional context
What i want to achieve is my users only have to see one menu, and not be changing between different designs. I can't recreate the entire site because it has a lot of list with powerautomate flows.
Could you please run these commands and post the output ?
$pagename = "Welcome"
$page = Add-PnPPage -Name $pagename -LayoutType Home -ErrorAction:Stop
(Get-PnPException).Exception | Select-Object *
If I run this on a site without "Site Pages" library and without any library of type "Page library", like a brand new site with "Publishing Portal" template, this is the error I get:
If I then create the Page library, this is what I get:
This follow-up issue happens because somehow there's no field called _AuthorByline.
@jansenbe should Page.EnsurePagesLibraryAsync() method in PnP Core at https://github.com/pnp/pnpcore/blob/40b870c5170b3d824dc278414d889c86c32f8037/src/sdk/PnP.Core/Model/SharePoint/Pages/Internal/Page.cs#L475 create a Page library if none exists ? There's no call to the CSOM method ListCollection.EnsureSitePagesLibrary() to create the library when missing and the method returns null in that case.
Could you please run these commands and post the output ?
$pagename = "Welcome" $page = Add-PnPPage -Name $pagename -LayoutType Home -ErrorAction:Stop (Get-PnPException).Exception | Select-Object *If I run this on a site without "Site Pages" library and without any library of type "Page library", like a brand new site with "Publishing Portal" template, this is the error I get:
If I then create the Page library, this is what I get:
This follow-up issue happens because somehow there's no field called
_AuthorByline.
Yes, i have exactly same errors
@jansenbe should
Page.EnsurePagesLibraryAsync()method in PnP Core at https://github.com/pnp/pnpcore/blob/40b870c5170b3d824dc278414d889c86c32f8037/src/sdk/PnP.Core/Model/SharePoint/Pages/Internal/Page.cs#L475 create a Page library if none exists ? There's no call to the CSOM method ListCollection.EnsureSitePagesLibrary() to create the library when missing and the method returns null in that case.
Can i create the library manually some way? Thanks
Having the exact same issue with the dotnet sdk, any workarounds?
https://www.sharepointdiary.com/2021/06/create-site-pages-library-in-sharepoint-online.html
Maybe this helps ? You might have to activate the feature manually.
I submitted a possible fix (activating the "Site Pages" feature if not active) in the PnP Core repository. I guess we could activate it in the Commandlet too, but feels much cleaner if it's done in the right place.
https://github.com/pnp/pnpcore/pull/1563

