Apply-PnPProvisioningTemplate : Cannot add part for the specified URI because it is already...

Brass Contributor
Apply-PnPProvisioningTemplate : Cannot add part for the specified URI because it is already in the package.

I get this error when I try to apply a provisioning template that I have modified in PowerShell (see bottom of post).

 

I'm using Get-PnPProvisioningTemplate to make a copy of a Communication site, but as described in this thread (link), when I try to Apply the template, there's a buggy PnPField element that causes the process to fail. I can get the template to work by manually unzipping the PNP, removing the buggy PnPField element, then zipping it back up again, but I need it to happen automatically. Hence the script below.

 

However, after executing the below then applying the template, I get the error above.

 

It seems like the zipping process is making the PNP file incompatible with the cmdlet for some reason. I've compared file sizes, and when using the script to compress the folder back into the PNP file, it results in a file that's 1 KB smaller than if I compress it manually.

 

Thoughts?

 

#User-editable variables
$templateSiteUrl = "https://MYTENANT.sharepoint.com/sites/pstemplate1"
$templateFolder = "C:\temp"
$templateFileName = "provTemplate.pnp"
$title = "TestC"
$url = "testC"
$description = "test"
$xmlBugId = "{1a53ab5a-11f9-4b92-a377-8cfaaf6ba7be}"

#Other variables
if ($creds -eq $null) {
$creds = Get-Credential
}
$templatePath = "$templateFolder\$templateFileName"

#Create the provisioning template
Connect-PnPOnline $templateSiteUrl -Credentials $creds
Get-PnPProvisioningTemplate -Out $templatePath -Force
Disconnect-PnPOnline
Write-Host Provisioning template created

#Fix the provisioning template
Add-Type -AssemblyName System.IO.Compression.FileSystem
$temporaryFolder = "$templateFolder\temporaryFolder"
Rename-Item -Path $templatePath -NewName "$templatePath.zip"
Expand-Archive -Path "$templatePath.zip" -DestinationPath $temporaryFolder
Rename-Item -Path "$templatePath.zip" -NewName ("$templatePath" + ".original.pnp") -Force
$xmlFilePath = "$temporaryFolder\Files\" + (Get-ChildItem -Path "$temporaryFolder\Files" | ? {$_.Extension -eq ".xml"}).Name
[xml]$xml = Get-Content -Path $xmlFilePath
$nodes = $xml | Select-XML -Xpath "//*[@ID='$xmlBugId']"
foreach ($node in $nodes) {
if ($node.Node.ParentNode.ChildNodes.Count -eq 1) {
$delete = $node.Node.ParentNode.ParentNode.RemoveChild($node.Node.ParentNode)
Write-Host Node and parent node deleted
}
else {
$delete = $node.Node.ParentNode.RemoveChild($node.Node)
Write-Host Node deleted
}
}
$xml.Save($xmlFilePath)
Write-Host Nodes removed and XML updated
Compress-Archive -Path "$temporaryFolder\*" -DestinationPath "$templatePath.zip"
Rename-Item -Path "$templatePath.zip" -NewName $templatePath
Write-Host PNP file rebuilt
Remove-Item -Path $temporaryFolder -Recurse -Force
Write-Host Temporary folder deleted

 

0 Replies