“SPSolutionExporter: System.InvalidOperationException: The Writer is closed or in error state” when saving site as template

I ran into the above error when saving a collaboration site as template using the OOTB SharePoint 2010 UI. The full stack is as below. This post describes how I solved this issue.

SPSolutionExporter: System.InvalidOperationException: The Writer is closed or in error state.    
 at System.Xml.XmlWellFormedWriter.AdvanceState(Token token)   
 at System.Xml.XmlWellFormedWriter.WriteEndElement()   
 at Microsoft.SharePoint.ScopedXmlWriterElement.Closer(XmlWriter writer, ScopedObjectUsage`1 wrapper)   
 at Microsoft.SharePoint.ScopedObjectUsage`1.Dispose(Boolean isDisposing)   
 at Microsoft.SharePoint.ScopedObjectUsage`1.Dispose()   
 at Microsoft.SharePoint.SPSolutionExporter.ExportFields(SPFieldCollection fields, String partitionName)   
 at Microsoft.SharePoint.SPSolutionExporter.ExportLists()   
 at Microsoft.SharePoint.SPSolutionExporter.GenerateSolutionFiles()   
 at Microsoft.SharePoint.SPSolutionExporter.ExportWebAsSolution()

Note that one of the calls in the stack is SPSolutionExporter.ExportFields(SPFieldCollection, ..). Using ILSpy you can see that this is being called for SPWeb.AvailableFields where SPWeb is the site you are saving. This means all fields “available” to the site are being exported.

First, change the logging level in Central Admin so we can get more info about what’s going on. When saving a site as template, activities are logged under the category SharePoint Foundation \ General. Change this to Verbose.

Run ULS Viewer and perform the save again. Using the correlation ID of the error, you will now see these two additional lines in the ULS just before the error:

SPSolutionExporter: Creating output stream for XML file at ‘C:\Users\TEMP.[yourDomain].002\AppData\Local\Temp\SPSolutionExporter-f472347a-8001-4756-8949-604f4ccad08c\TEST2ListInstances\ElementsFields.xml’

SPSolutionExporter: Closing XmlWriter ‘TEST2ListInstances\ElementsFields.xml’.

The first line indicates the physical location where the WSP (i.e. your site template) is being saved. The second line indicates the file that is causing the error. Note ‘TEST2ListInstances‘ – ‘TEST2’ is the name I specified for my site template.

Go to the physical location and open the ElementsFields.xml file. In my case, this file was incomplete and therefore the XML is malformed. The last field (i.e. the incomplete field) in the file appeared as below:

<Field Type="MultiChoice" ID="{faeb36f0-dca0-4636-a0a1-936353e1076a}" Name="MyField" DisplayName="My Field" Group="My List Columns" Required="TRUE" FillInChoice="FALSE" ShowInDisplayForm="FALSE" ShowInViewForms="FALSE" xmlns="http://schemas.microsoft.com/sharepoint/" SourceID="{1349b558-1773-4441-b7ab-1df02509bc8e}" StaticName="MyField" EnforceUniqueValues="FALSE" Indexed="FALSE" Overwrite="TRUE">
		<CHOICES>
			<CHOICE

I checked the SchemaXml property of the offending field in SharePoint Manager, and it appeared as below:

<Field Type="MultiChoice" ID="{faeb36f0-dca0-4636-a0a1-936353e1076a}" Name="MyField" DisplayName="My Field" Group="My List Columns" Required="TRUE" FillInChoice="FALSE" ShowInDisplayForm="FALSE" ShowInViewForms="FALSE" xmlns="http://schemas.microsoft.com/sharepoint/" SourceID="{1349b558-1773-4441-b7ab-1df02509bc8e}" StaticName="MyField" EnforceUniqueValues="FALSE" Indexed="FALSE" Version="4">
  <CHOICES>
    <CHOICE xmlns="">Some text</CHOICE>
  </CHOICES>
</Field>

The problem is the xmlns=”” part of the CHOICE element. I edited the SchemaXml property of the field straight in SharePoint Manager (do this in Dev environment only!!) and removed the xmlns=”” part. I was then able to successfully save the site as template.

There is more work to be done at this point, because if you now edit the field using the OOTB SharePoint UI, it will add the xmlns=”” part back in (even if you just click OK and didn’t make any update using the SharePoint UI).

To stop SharePoint from doing this, remove the xmlns=”http://schemas.microsoft.com/sharepoint/&#8221; part from the parent Field element.

The offending field was deployed through XML. I went back and look at the source code and both the Field and the CHOICE elements do not have the xmlns attribute specified. Not sure how that attribute made it into the SharePoint DB – another mystery.

Advertisements

About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.
This entry was posted in Field, SharePoint 2010, Site Column, Site Template. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s