Adventures in PnP PowerShell provisioning templates

Note: This is a crosspost from Mike Homol’s personal blog. See the original post here.

Has this ever happened to you? I had built a custom list with a custom view. To be more precise, I had basically lifted Chris Kent’s sample here for a custom FAQ and dropped this on the page and the client was thrilled with it just as it is. Thanks Chris! Here’s what the FAQ page looked like on the template site:

But this is just the beginning! This was my template. I need lots of sites to have this same FAQ page as a starting point, and it needs to look this good too.

So, onto provisioning with Powershell and PnP! At first I was running this:

Get-PnPProvisioningTemplate -Out $templateFull -Verbose -PersistBrandingFiles `
-PersistPublishingFiles -IncludeAllClientSidePages -Force

Apply-PnPProvisioningTemplate -Path $templateFull

Looks familiar right? Well pretty much everything was working great except for this:

What the heck was going on here? The view and the list were migrating just fine, but that view was not getting applied! Or, was it? I noticed this in the List part properties:

See anything off? Nothing is selected in the view drop down, even though it is selected in the my template site.

Acting on a Hunch

So here was my hunch. Perhaps, the pages are getting deployed before the custom list and custom view, sooo when the page gets made, there’s no view to select, which is why it looks like the above. I acted on this hunch, by doing the following:

I split out just the FAQ list portion from the full Get-PnPProvisioningTemplate – essentially doing 2 Gets: one for the lists only and one for everything else. Here’s what that looked like:

Get-PnPProvisioningTemplate -Out $templateFull -Verbose -PersistBrandingFiles `
-PersistPublishingFiles -IncludeAllClientSidePages -Force
Get-PnPProvisioningTemplate -Out $templateListOnly -Verbose -Handlers Lists `
-ListsToExtract "FAQ" -Force

Now you have 2 files. But there’s 1 trick to this if you want it to work in your favor. You need to open up the XML file for everything and delete just the ListInstance node for the list (in my case, FAQ) from the XML file. So you can’t easily do this all in one full script. You’d have to keep your pulls separate from your applies because of this manual intervention.

Then I applied my 2 files separately as well, starting with the lists first:

Apply-PnPProvisioningTemplate -Path $templateListOnly

Apply-PnPProvisioningTemplate -Path $templateFull

And, viola! My FAQ list was displaying as expected on the page, because the view was already found for the web part property because it already existed.

Share and Enjoy !

Related Content: