pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
990 stars 663 forks source link

How to Get Pnp Tenant Template? #2587

Open VishnuMohan259 opened 4 years ago

VishnuMohan259 commented 4 years ago

Hi , Tried to get tenant template of a site but getting different errors

1.Tried to connect with hub site and executed the code ,Got 403 error. 2.After that Connect to Tenant site and executed the code ,got some other error please find the attachment TenantTemplate

Regards

Vishnu Mohan

ghost commented 4 years ago

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

Seasto-Work commented 4 years ago

Keep getting Object reference not set to an instance of the object when trying to use basic Get-PnPTenantTemplate... image

AnoopNagappan commented 4 years ago

Good day to you both @VesaJuvonen @waldekmastykarz Could you please guide us to address this issue..

Seasto-Work commented 4 years ago

The credentials I had used in the above Screenshot had Tenant Level Permissions (SharePoint Admin), logged in using the User Account Name and Password using a System.Management.Automation.PSCredential object

I was using this documentation: https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnptenanttemplate?view=sharepoint-ps, as in general there is very sparse amounts of documentation on using Tenant Templates available everything seems to be on Provisioning Templates.

Benny183 commented 4 years ago

Same Error here also in Apply-PnP Tenant Template

muhammadali-shah commented 4 years ago

Hi,

did this issue got resolved? If yes, could you please share the solution? I am also getting the Object reference not set to an instance of an object with GET-PnpTenantTemplate...

image

AnoopNagappan commented 4 years ago

@muhammadali-shah Please look into your Azure AD -> Enterprise Applications -> PnP.PowerShell -> Permissions, you can see the difference ... Probably No admin consented permissions? permission

What I tried to communicate is that, you might be need an admin consent for PnP.PowerShell , hopefully then PnPTenantTemplate will work for you..

For me its working fine now

joelpereira commented 4 years ago

I'm also getting the same error: Get-PnPTenantTemplate : Object reference not set to an instance of an object. Are we using "Get-PnPTenantTemplate" cmd incorrectly, or is this an issue?

joelpereira commented 4 years ago

After investigating the code, it looks like if a -Configuration is not provided, it will throw the NullReference exception. Until this is fixed in code, you can just add a configuration file to the Get-TenantTemplate cmdlet and it should work.

Sample config.json (https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/configuring-the-pnp-provisioning-engine):

{
    "$schema":"https://aka.ms/sppnp-extract-configuration-schema",
    "persistAssetFiles": true,
    "handlers": [
        "Lists",
        "WebSettings",
        "Pages"
    ],
    "pages": {
        "includeAllClientSidePages": true
    }
}

Command:

PS> Connect-PnPOnline ...
PS> Get-PnPTenantTemplate -Configuration config.json -Out backupfile.xml

For the devs: https://github.com/pnp/PnP-PowerShell/blob/master/Commands/Provisioning/Tenant/GetTenantTemplate.cs ExecuteCmdlet()

ExtractConfiguration extractConfiguration = null;
...
(line 84) extractConfiguration.FileConnector = fileSystemConnector;

(extractConfiguration is null, so it throws the null reference exception)

Side note: The namespace also seems to be pointing to ...Provisioning.Site instead of Provisioning.Tenant

cwdata commented 4 years ago

Hi @joelpereira thanks for your investigations but it still doesn't work. The error (null reference exception) stays the same, even when using the config.json you provided. And you must provide the -SiteUrl parameter, otherwise how should the system know which site you want to extract? Seems there is still something missing…

joelpereira commented 4 years ago

Hi @cwdata ; You're right about the -SiteUrl; sorry about that. I can't confirm without seeing the full command you're using (without personal links/info).

Can you try to update to the latest version of pnp-sharepoint (there were 2 new updates just today for example) and try it again. I just tried it with the latest version (SharePointPnPPowerShellOnline 3.22.2006.1) and it worked nicely. Link to update and view your version: https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets?view=sharepoint-ps

This produced a nice looking XML file: Get-PnPTenantTemplate -Configuration '.\config.json' -Out tenantBackup.xml -SiteUrl https://TENANT.sharepoint.com/sites/SITENAME

Fyi, I haven't tried importing it, so not sure of the quality of the export file yet. Also make sure you're using PowerShell v5 (7 was giving me issues before).

AnoopNagappan commented 4 years ago

@joelpereira thank you for sharing and unfortunately could not succeeded
image

cwdata commented 4 years ago

@joelpereira, I'm using the same versions as you: image If I directly connect to the site I want to extract I get 'access denied' like @AnoopNagappan If I connect to the admin site I get the null reference exception. The full exception from Get-PnPException is as follows:

CorrelationId    : 8cf45a9f-70c7-b000-5ae5-fec8baf31a71
TimeStampUtc     : 10.06.2020 07:22:35
Message          : Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
Stacktrace       :    bei 
                   OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers.PnPProvisioningContext.AcquireToken(String 
                   resource, String scope)
                      bei Microsoft.SharePoint.Client.ClientContextExtensions.<>c__DisplayClass11_0.<Clone>b__2(Object 
                   sender, WebRequestEventArgs args)
                      bei System.EventHandler`1.Invoke(Object sender, TEventArgs e)
                      bei Microsoft.SharePoint.Client.ClientRuntimeContext.OnExecutingWebRequest(WebRequestEventArgs args)
                      bei Microsoft.SharePoint.Client.ClientContext.GetWebRequestExecutor()
                      bei Microsoft.SharePoint.Client.ClientContext.<GetFormDigestInfoPrivateAsync>d__b.MoveNext()
                   --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
                      bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                      bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                      bei Microsoft.SharePoint.Client.ClientContext.<EnsureFormDigestAsync>d__8.MoveNext()
                   --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
                      bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                      bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                      bei Microsoft.SharePoint.Client.ClientContext.<ExecuteQueryAsync>d__4.MoveNext()
                   --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
                      bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                      bei Microsoft.SharePoint.Client.ClientContextExtensions.<ExecuteQueryImplementation>d__7.MoveNext()
                   --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
                      bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                      bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                      bei 
                   Microsoft.SharePoint.Client.ClientObjectExtensions.<EnsurePropertiesImplementation>d__8`1.MoveNext()
                   --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
                      bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                      bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                      bei Microsoft.SharePoint.Client.ClientObjectExtensions.EnsureProperties[T](T clientObject, 
                   Expression`1[] propertySelector)
                      bei OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers.ObjectHierarchySequenceSites.ExtractObje
                   cts(Tenant tenant, ProvisioningHierarchy hierarchy, ExtractConfiguration configuration)
                      bei OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers.SiteToTemplateConversion.GetTenantTempla
                   te(Tenant tenant, ExtractConfiguration configuration)
                      bei SharePointPnP.PowerShell.Commands.Provisioning.Site.GetTenantTemplate.ExtractTemplate(ExtractCon
                   figuration configuration)
                      bei SharePointPnP.PowerShell.Commands.Provisioning.Site.GetTenantTemplate.ExecuteCmdlet()
                      bei SharePointPnP.PowerShell.Commands.PnPSharePointCmdlet.ProcessRecord()
ScriptLineNumber : 1

We are using MFA. In some other thread I've read that PnP Powershell has some problems when you are not using a simple login with username/password. Maybe that's also the issue here.

joelpereira commented 4 years ago

Hi @AnoopNagappan ; that looks like a 403 Forbidden error, so check your permissions and whether you have the right access to the site (SharePoint admin?). Also, check the site template type (see below).

Hi @cwdata ; Yup, it looks like that's a new null exception. Couple of things to try:

I've noticed that it throws a similar null exception if there's something wrong with the configuration file. Ex. -if it's can't find or access the file (check the path/name) -if it's not a valid JSON file

It will also throw a similar null exception if the site you're trying to export is a classic type (rather than modern). Run the following command to pull the site template type: Get-PnPTenantSite -Url <URL>

It should be one of the following, otherwise you'll likely get a null error: SITEPAGEPUBLISHING#0 GROUP#0 STS#3

Hope this helps.

cwdata commented 4 years ago

Hi @joelpereira, thanks for your suggestions. The config file is an exact copy & paste version from your example and the site is a newly created modern communication site (SITEPAGEPUBLISHING#0). As the stacktrace I posted says that the exception occured in the AcquireToken() method I still suspect authentication problems when MFA is enabled. I couldn't try without because this is forbidden in our tenant.