SharePoint Online
PSVersion 7.1.0
PnP.PowerShell - 0.3.29
When trying to create a PnP provisioning template which has PageContents specified in the list of handlers the process starts and then errors with the message: Get-PnPSiteTemplate: Object reference not set to an instance of an object.

I have tried this on a number of different machines and different site types Team Site, Communication Site, Team Site (no Group) all with the same result. I have also tried outputting the template as both an xml and pnp file.
If I exclude PageContents from the handlers and execute Get-PnPSiteTemplate the template is created.
If I use Get-PnPProvisioningTemplate in SharePointPnPPowerShellOnline v3.28.2012.0 and specify PageContents in the handler the command completes as expected and the template is produced so the issue does not appear to be with the site I am trying to template.
Can you call Get-PnPException immediately after the error and share that output?
OK, I'll do that.
I ran Get-PnPException and here is the output:
Message : Object reference not set to an instance of an object.
Stacktrace : at PnP.Framework.PnPCoreSdkAuthenticationProvider.get_RequiresCookieAuthentication()
at PnP.Core.Services.BatchClient.ProcessSharePointRestAuthentication(Uri site, HttpRequestMessage request)
at PnP.Core.Services.BatchClient.ExecuteSharePointRestInteractiveAsync(Batch batch)
at PnP.Core.Services.BatchClient.ExecuteBatch(Batch batch)
at PnP.Core.Model.BaseDataModel`1.RequestAsync(ApiCall apiCall, HttpMethod method)
at PnP.Core.Services.PnPContextFactory.InitializeContextAsync(PnPContext context)
at PnP.Core.Services.PnPContextFactory.CreateAsync(Uri url, IAuthenticationProvider authenticationProvider)
at PnP.Core.Services.PnPContextFactory.Create(Uri url, IAuthenticationProvider authenticationProvider)
at PnP.Framework.PnPCoreSdk.GetPnPContext(ClientContext context)
at Microsoft.SharePoint.Client.PageExtensions.LoadClientSidePage(Web web, String pageName)
at PnP.Framework.Provisioning.ObjectHandlers.Utilities.ClientSidePageContentsHelper.ExtractClientSidePage(Web web, ProvisioningTemplate template, ProvisioningTemplateCreationInformation
creationInfo, PnPMonitoredScope scope, PageToExport page)
at PnP.Framework.Provisioning.ObjectHandlers.ObjectClientSidePageContents.ExtractObjects(Web web, ProvisioningTemplate template, ProvisioningTemplateCreationInformation creationInfo)
at PnP.Framework.Provisioning.ObjectHandlers.SiteToTemplateConversion.GetRemoteTemplate(Web web, ProvisioningTemplateCreationInformation creationInfo)
at Microsoft.SharePoint.Client.WebExtensions.GetProvisioningTemplate(Web web, ProvisioningTemplateCreationInformation creationInfo)
at PnP.PowerShell.Commands.Provisioning.Site.GetSiteTemplate.ExtractTemplate(XMLPnPSchemaVersion schema, String path, String packageName, ExtractConfiguration configuration) in
D:\a\powershell\powershell\src\Commands\Provisioning\Site\GetSiteTemplate.cs:line 397
at PnP.PowerShell.Commands.Provisioning.Site.GetSiteTemplate.ExecuteCmdlet() in D:\a\powershell\powershell\src\Commands\Provisioning\Site\GetSiteTemplate.cs:line 149
at PnP.PowerShell.Commands.PnPSharePointCmdlet.ProcessRecord() in D:\a\powershell\powershell\src\Commands\Base\PnPSharePointCmdlet.cs:line 59
ScriptLineNumber : 1
I think we fixed that today, can you retest with the next nightly build (so version 0.1.106)?
I updated to the latest version of the PnP.PowerShell module 0.3.30 (which I assume must include the 0.1.106 build) and the Get-PnPSiteTemplate now creates the template as expected.
Thanks for your help
This issue has since resurfaced in pnp.powershell 0.3.34. When running Invoke-PnPSiteTemplate using v0.3.34 I get the following error, however, if I switch back to v0.3.30 the template is applied as expected.
Message : Object reference not set to an instance of an object.
Stacktrace : at PnP.PowerShell.Commands.Provisioning.Site.InvokeSiteTemplate.<>c.<
D:\a\powershell\powershell\src\Commands\Provisioning\Site\InvokeSiteTemplate.cs:line 335
--- End of stack trace from previous location ---
at PnP.Framework.Provisioning.ObjectHandlers.PnPProvisioningContext.AcquireToken(String resource, String scope)
at Microsoft.SharePoint.Client.ClientContextExtensions.GetAccessToken(ClientRuntimeContext clientContext)
at Microsoft.SharePoint.Client.ClientContextExtensions.IsAppOnly(ClientRuntimeContext clientContext)
at PnP.Framework.Provisioning.ObjectHandlers.ObjectListInstance.UpdateList(Web web, List existingList, ListInstance templateList, TokenParser parser, PnPMonitoredScope scope, Boolean
isNoScriptSite)
at PnP.Framework.Provisioning.ObjectHandlers.ObjectListInstance.ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation
applyingInformation)
at PnP.Framework.Provisioning.ObjectHandlers.SiteToTemplateConversion.ApplyRemoteTemplate(Web web, ProvisioningTemplate template, ProvisioningTemplateApplyingInformation
provisioningInfo, Boolean calledFromHierarchy, TokenParser tokenParser)
at Microsoft.SharePoint.Client.WebExtensions.ApplyProvisioningTemplate(Web web, ProvisioningTemplate template, ProvisioningTemplateApplyingInformation applyingInformation)
at PnP.PowerShell.Commands.Provisioning.Site.InvokeSiteTemplate.ExecuteCmdlet() in D:\a\powershell\powershell\src\Commands\Provisioning\Site\InvokeSiteTemplate.cs:line 376
at PnP.PowerShell.Commands.PnPSharePointCmdlet.ProcessRecord() in D:\a\powershell\powershell\src\Commands\Base\PnPSharePointCmdlet.cs:line 59
ScriptLineNumber : 1
How are you connecting? Credentials, ClientId / Certificate or ClientId/Thumbprint?
Locally I was using PnPManagementShell and in my Azure Function I was using ClientId, Thumbprint and Tenant. Both failed with the same error.
would it be possible that you share a page from your template with us? e.g. the xml of the page, so we can try to reproduce it.
the confusing part is that we did not touch that code since version 0.3.30... so I'm a bit puzzled why it shows up suddenly again with 0.3.34...
No problem:
I also get what looks like the same error with Get-PnPSiteTemplate if I specify Lists in the Handlers
Message : Object reference not set to an instance of an object.
Stacktrace : at PnP.PowerShell.Commands.Provisioning.Site.GetSiteTemplate.<>c.<
D:\a\powershell\powershell\src\Commands\Provisioning\Site\GetSiteTemplate.cs:line 376
--- End of stack trace from previous location ---
at PnP.Framework.Provisioning.ObjectHandlers.PnPProvisioningContext.AcquireToken(String resource, String scope)
at Microsoft.SharePoint.Client.ClientContextExtensions.GetAccessToken(ClientRuntimeContext clientContext)
at Microsoft.SharePoint.Client.ClientContextExtensions.IsAppOnly(ClientRuntimeContext clientContext)
at PnP.Framework.Provisioning.ObjectHandlers.ObjectListInstance.ExtractObjects(Web web, ProvisioningTemplate template,
ProvisioningTemplateCreationInformation creationInfo)
at PnP.Framework.Provisioning.ObjectHandlers.SiteToTemplateConversion.GetRemoteTemplate(Web web, ProvisioningTemplateCreationInformation creationInfo)
at Microsoft.SharePoint.Client.WebExtensions.GetProvisioningTemplate(Web web, ProvisioningTemplateCreationInformation creationInfo)
at PnP.PowerShell.Commands.Provisioning.Site.GetSiteTemplate.ExtractTemplate(XMLPnPSchemaVersion schema, String path, String packageName,
ExtractConfiguration configuration) in D:\a\powershell\powershell\src\Commands\Provisioning\Site\GetSiteTemplate.cs:line 417
at PnP.PowerShell.Commands.Provisioning.Site.GetSiteTemplate.ExecuteCmdlet() in
D:\a\powershell\powershell\src\Commands\Provisioning\Site\GetSiteTemplate.cs:line 149
at PnP.PowerShell.Commands.PnPSharePointCmdlet.ProcessRecord() in D:\a\powershell\powershell\src\Commands\Base\PnPSharePointCmdlet.cs:line 59
ScriptLineNumber : 1
we can reproduce with a devicelogin auth (which is what -PnPManagementShell is doing). We will work on a solution the coming days.
@Nibushi : can you check if things now work for you?
@jansenbe I checked using PnP.PowerShell build 0.3.38 and was able to successfully run Get-PnPSiteTemplate using device login auth (-PnPManagementShell).
I was also able to run Invoke-PnPSiteTemplate to apply the new template to a newly created site.
The issue appears to be resolved. Thanks for sorting that out 馃憤
Just an FYI for posterity, I was trying to setup a basic provisioning and had the same issue authenticating with -UseWebLogin but -Interactive fixed the issue
Most helpful comment
Just an FYI for posterity, I was trying to setup a basic provisioning and had the same issue authenticating with -UseWebLogin but -Interactive fixed the issue