[BUG] Add-PnPListFoldersToSiteTemplate: [...] invalid child element 'Preferences' [...]
Background
I'd like to add folders to a (simple) site provisioning template.
Expected behavior
I expect this code to work, to get the Documents list and to add the folders to the list:
Get-PnPSiteTemplate -Handlers ContentTypes,Lists -Out template.xml -ListsToExtract "Documents"
Add-PnPListFoldersToSiteTemplate -List "Documents" -Path template.xml
Actual behavior
I get this error:
Add-PnPListFoldersToSiteTemplate: The element 'Provisioning' in namespace 'http://schemas.dev.office.com/PnP/2021/03/ProvisioningSchema' has invalid child element 'Preferences' in namespace 'http://schemas.dev.office.com/PnP/2021/03/ProvisioningSchema'. List of possible elements expected: 'Templates, Sequence, Teams, AzureActiveDirectory, Drive, ProvisioningWebhooks' in namespace 'http://schemas.dev.office.com/PnP/2021/03/ProvisioningSchema'. Add-PnPListFoldersToSiteTemplate: Invalid template file.
This apparently is caused by the Preferences element in the template XML file:

Removing the Preferences element fixes the error. Maybe the schema is out of date? Or ist this a version mismatch (1.6 vs 1.7)?
PnP.PowerShell has version v1.7.0 (latest as of writing this)
I am seeing the same issue with Get-PnPTenantTemplate running as an Azure function, not locally...
Error: The element 'Provisioning' in namespace 'http://schemas.dev.office.com/PnP/2021/03/ProvisioningSchema' has invalid child element 'Preferences' in namespace 'http://schemas.dev.office.com/PnP/2021/03/ProvisioningSchema'. List of possible elements expected: 'Templates, Sequence, Teams, AzureActiveDirectory, Drive, ProvisioningWebhooks' in namespace 'http://schemas.dev.office.com/PnP/2021/03/ProvisioningSchema'.
PnP.PowerShell 1.7.0 running in Azure PowerShell functions.
Re-ordering the nodes in the XML manually solves the issue.
I am currently seeing the same issue Running 'PnP.PowerShell' version 1.11.0, I have tried running the following on multiple tenants all with the same issue:
$temp = New-PnPSiteTemplate Save-PnPSiteTemplate -Template $temp -Out ".\templates\pnptemplate.xml" Export-PnPListToSiteTemplate -Out ".\templates\pnptemplate.xml" -List "Company Updates" -Force Add-PnPListFoldersToSiteTemplate -Path ".\templates\pnptemplate.xml" -List "Company Updates"
As pointed on by @heinrich-ulbricht removing the 'Preferences' element in the file fixes the issue.
This issue is still happening randomly on Azure Functions using PowerShell core. If I remove the preferences element, applying the template says it is invalid. Since it is happening randomly, it makes it harder to debug.
Quick fix for those still running into this. I put it in my durable function code and it works like a champ. `function Move-PnpPreferences { param ( [string]$xmlFilePath )
[xml]$xml = Get-Content $xmlFilePath
$ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$ns.AddNamespace("pnp", "http://schemas.dev.office.com/PnP/2022/09/ProvisioningSchema")
$provisioning = $xml.SelectSingleNode("//pnp:Provisioning", $ns)
$preferences = $xml.SelectSingleNode("//pnp:Provisioning/pnp:Preferences", $ns)
if ($preferences) {
# Remove the Preferences element
$provisioning.RemoveChild($preferences) | Out-Null
# Find the insertion point just after the opening element
$insertAfterNode = $provisioning.ChildNodes | Where-Object { $_.NodeType -eq "Element" } | Select-Object -First 1
if ($insertAfterNode) {
$provisioning.InsertBefore($preferences, $insertAfterNode) | Out-Null
} else {
$provisioning.PrependChild($preferences) | Out-Null
}
# Save the changes back to the XML file
$xml.Save($xmlFilePath)
Write-Output "Preferences node moved successfully."
} else {
Write-Output "No Preferences node found."
}
}
Move-PnpPreferences -xmlFilePath C:\temp\broken.xml`