Apply-PnPProvisioningTemplate : Cannot access a closed Stream. At line:1 char:1 + Apply-PnPProvision


Todays issue


Hi am using the latest Powershell extension and each tilme i run Appl-PnPProvioningTemplate i get the error Apply-PnPProvisioningTemplate : Cannot access a closed Stream. At line:1 char:1 + Apply-PnPProvision. I enabled logging and i dont see anything that could hint the cause of the error. Same thing happen if I use c# code

18 Replies
It will be helpful to check this issue, if you can post the xml used.

please post the xml so we can look at what is happening.

This error message typically indicates a Checked-out file. There's probably a pnp:File definition in your template for a File that's checked out. Could this be your issue?

Followed instruction her and i found one file that was checked out. However i still get the error after checking it in. Each time i try to upload the .pnp file i am geting the error "THe contents of the attachment doesnt match its file type"


just a guess but where is the proviosing template? it is in a zip? as far as i know you can not use a zip to apply template.


maybe  i am wrong here but if you do a get template you get xml which you can provision back..


Since june 2016 release of PnP you can extract templates to a file with a .pnp extension which is effectively a ZIP file containing all provisioning artifacts

still having this issue. I am not sure if i mentioned that this is a publishinfg site. If the error is because of a checked out file how can i know what file is being checked out. I created over the site and every site assets i added i ensured that it was published and check in

If I understand you got a different error after you checked in the file. Before the file was checked in you got "Cannot access a closed stream". After checking in you got "THe contents of the attachment doesnt match its file type".


I'm not familiar with that last error message. When exactly do you get this message? Are you working against a SharePoint Online environment or on-premises? I

As it stands this is the only error I am getting. I am working against a SharePoint Online environment. Another quick question. Does the provisioning engine provision all the assets for the master page css, js files etc
Yes, the Provisioning Engine is able to provision all these types of files as long as they are defined correctly in your template

Is this something  I have to do manually or this  

Get-PnPProvisioningTemplate -PersistBrandingFiles

do all of that for me. If its manul can you point me in the right direction.


Thanks again guys for all your help 

PersistBrandingFiles should extract the MasterPage you applied, the sitelogo you configured, the alternate CSS you specified and all the files related to the applied Composed Look (if any). There are 2 more PersistFiles parameters. Any other file not targeted by these parameters should be added to the template manually.

See this page for all documentation on the cmdlet:

In order to replicate the issue try the following
1. Create two site collection using the publishing template one will be used to apply your custom branding and the other is the one you will use powershell to apply said branding
2. Create a simple html file and call it whatever you want. in my case I called it simple.html
my file only had this
<!DOCTYPE html>
<title>Page Title</title>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

3. Use design manger to upload this template to the Master Page Gallery of the site collection you want to apply branding to.
4. Go to Edit Master Pages in Design Manager and then click Convert an HTML file to a SharePoint master page.
5. Select the html file you uploaded earlier and this will convert your file to master page file.
6. In design manager gor to Publish and Apply design the click on go o the master page gallery.
7. Go to were you had uploded you html file and you will see the .master file. Ensure all files are published and checked in.
8. Go to site Settings and click on Master page. In the drop downbox select your newly created master page. This will apply you custom branding.
Basically i followed the steps here using the simpliest of html file with no site assets ""

The next steps are to now get the provisioning template using powershell from your branded site the use powershell to apply the branding to you other site collection (make sure it was created using publishing template)

I used the following parameters when I was getting the publishing template -PesistBrandingFiles -PersistPublishing Files. I find that if i dont use PersistPublishingFiles it doesnt provision my html file.

As far you can see what i am doing is basic and its not working

best response confirmed by Debayo Graham (Contributor)

I've followed the steps you took and I got the same error as you got. The file causing the error was simple.master. During Provisioning PnP is trying to overwrite the file while it is checked out during conversion of the simple.html file to a master page.


I was able to fix the error and get the desired results following the steps below:


  1. Rename your .pnp file to a .zip file
  2. Extract the contents of the file
  3. In the extracted files folder locate your template file in the Files folder (it's named [someguid].xml)
  4. Open the file in your favorite XML editor
  5. Locate the pnp:file element for your .master file and remove the element
  6. Save your XML file
  7. ZIP the contents of your folder again and give it the same name as the original .pnp file you extracted
  8. Apply the .pnp file to your site


I consider this a bug in PnP and I'd suggest you to submit an issue in PnP describing your steps to reproduce and my steps to work around this.


Hope this helps!

For anyone who stumble up on this error please follow up on the issue report here: