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/” 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.