SOLVED

Copy site from one tenant to another using a template is giving an "access denied" error

Brass Contributor

Hello everyone,

 

I need to replicate a site from one tenant to another. For that, I'm following the steps in C-Sharp Corner, which consists in two simple code snippets:

Export site template from original tenant

#Parameters
$SourceSiteURL = "https://qhdm.sharepoint.com/sites/MyTeamsites"
$XMLTemplatePath = "C:\Site Templates\DemoSiteTemplate.xml"

#Connect to source SharePoint
Connect-PnPOnline -Url $SourceSiteURL -Interactive

#Export site template to specified path
Get-PnPSiteTemplate -Out ($XMLTemplatePath)

#Disconnect source SharePoint
Disconnect-PnPOnline


Import site template to destination tenant

#Parameters
$DestinationSiteURL = "https://qhdm.sharepoint.com/sites/DestinationDemoSites"
$XMLTemplatePath = "C:\Site Templates\DemoSiteTemplate.xml"

#Connect to PnP Online
Connect-PnPOnline -Url $DestinationSiteURL -Interactive

#Apply SharePoint template
Invoke-PnPSiteTemplate -Path $XMLTemplatePath -ClearNavigation

#Disconnect source SharePoint
Disconnect-PnPOnline


Exporting the template works great; however, when trying to apply the template in the destination site, the following error appears:

Invoke-PnPSiteTemplate : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
+ Invoke-PnPSiteTemplate -Path $XMLTemplatePath -ClearNavigation
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-PnPSiteTemplate], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,PnP.PowerShell.Commands.Provisioning.Site.InvokeSiteTemplate

I have admin permissions on both tenants already. What could possibly be causing this error?

2 Replies
best response confirmed by jbtzz (Brass Contributor)
I solved this by actually deleting the faulty lines from the generated template (pointed out by adding this line to the code, before the Invoke-PnPSiteTemplate line: Set-PnPTraceLog -On -LogFile "C:\log.txt" -Level debug).

However, I'm marking this as the best response as it's a valid workaround and could help others.
1 best response

Accepted Solutions
best response confirmed by jbtzz (Brass Contributor)