powershell icon indicating copy to clipboard operation
powershell copied to clipboard

Add-PnPPage: Object reference not set to an instance of an object when trying to create a new page

Open borjaevo opened this issue 1 year ago • 7 comments

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:

  1. Im using powershell 7 and PnP installed yesterday version 2.4.0
  2. Auth using Connect-PnPOnline -Url "https://----.sharepoint.com/calidad" -Credential (Get-Credential)
  3. Execute command $pagename = "Welcome"
  4. Execute command $page = Add-PnPPage -Name $pagename -LayoutType Home
  5. 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.

borjaevo avatar May 15 '24 07:05 borjaevo

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: image

If I then create the Page library, this is what I get: image

This follow-up issue happens because somehow there's no field called _AuthorByline.

jackpoz avatar May 19 '24 15:05 jackpoz

@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.

jackpoz avatar May 19 '24 16:05 jackpoz

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: image

If I then create the Page library, this is what I get: image

This follow-up issue happens because somehow there's no field called _AuthorByline.

Yes, i have exactly same errors

borjaevo avatar May 21 '24 08:05 borjaevo

@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

borjaevo avatar May 21 '24 09:05 borjaevo

Having the exact same issue with the dotnet sdk, any workarounds?

Jefwillems avatar Aug 13 '24 14:08 Jefwillems

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.

gautamdsheth avatar Sep 28 '24 18:09 gautamdsheth

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

koskila avatar Oct 26 '24 15:10 koskila