I am using .NET 6.0, OpenXML 2.20, and PnP.Framework 1.13 to programmatically create a .docx file from a template and upload it to SharePoint On Premise 2016 for viewing in Office Online Server 2016. The document opens fine in view mode, but editing in the browser throws an error.
Here’s the process:
A generic .docx template is broken down into multiple sections, each represented by its own .docx. Data is populated into these sections, and their contents are copied into the main template.
The populated template is saved and uploaded to SharePoint. It previews fine in view mode.
When attempting to edit in the browser, I encounter an error logged by OOS:
2024-06-11 10:29:39.15 w3wp.exe#WebWordEditor (0x20E8) 0x1750 Hosted Office Online Word Online File Open bwd3h Exception
[Type: System.InvalidOperationException] System.InvalidOperationException: Cannot load element at current status.
at Microsoft.Office.WordCompanion.OpenXml.ContentBlockReader.Load()
at Microsoft.Office.Web.WordEditor.Transform.ParaContentTransform.IsSkippedReaderSupported(ContentBlockReader readerChild, Metrics metrics)
at Microsoft.Office.Web.WordEditor.Transform.ParaContentTransform.TransformChildren(Int32 paragraphId, ContentBlockReaderStack readerStack, Int32& runCountOnFieldBegin, UInt32& runId, Boolean isBeginningOfParagraph)
at Microsoft.Office.Web.WordEditor.Transform.ParaContentTransform.Transform(ContentBlockReader reader)
at Microsoft.Office.Web.WordEditor.Transform.TopLevelTransform.TransformChildren(ContentBlockReader reader)
at Microsoft.Office.Web.WordEditor.Transform.TableCellContentTransform.Transform(ContentBlockReader reader)
at Microsoft.Office.Web.WordEditor.Transform.TableContentTransform.TransformTableCell(TableCellReader cellReader, Int32 cellCount, Int32 gridSpan, TableBorders tableBorders)
at Microsoft.Office.Web.WordEditor.Transform.TableContentTransform.TransformTableOrGhostCells(TableCellReader cellReader, Int32 cellCount, TableBorders tableBorders)
at Microsoft.Office.Web.WordEditor.Transform.TableContentTransform.TransformTableRow(TableRowReader rowReader)
at Microsoft.Office.Web.WordEditor.Transform.TableContentTransform.Transform(ContentBlockReader reader)
at Microsoft.Office.Web.WordEditor.Transform.TopLevelTransform.TransformChildren(ContentBlockReader reader)
at Microsoft.Office.Web.WordEditor.Transform.BodyContentTransform.Transform(ContentBlockReader reader)
at Microsoft.Office.Web.WordEditor.Transform.MainDocumentTransform.Transform(ContentBlockReader reader)
at Microsoft.Office.Web.WordEditor.Transform.DocumentTransform.CreateTopLevelOutlines()
at Microsoft.Office.Web.WordEditor.Transform.DocumentTransform.TransformDocument()
GUID: 26723129-6fa6-4e89-b019-68f15e2ab069
This issue does not occur if I open the document in Office 365, make a minor edit, save it, and then upload it to OOS, suggesting a compatibility or formatting issue introduced by OpenXML.
Code for Inserting Sections body into Main Document. I feel that this is the main culprit but I am not sure what to adjust here.
public static void ReplaceParagraphWithBodyContent(
this WordprocessingDocument doc,
Body body,
string paragraphName)
{
var paragraphs = doc.MainDocumentPart.Document.Body.Elements<Paragraph>();
var targetParagraph = paragraphs.FirstOrDefault(p => p.InnerText.Equals(paragraphName, StringComparison.InvariantCultureIgnoreCase))
?? throw new ObjectNotFoundException($"Paragraph {paragraphName} was not found");
if (body != null)
{
var elements = body.Elements()
.Where(x => x is not SectionProperties)
.ToArray();
foreach (var element in elements)
{
doc.MainDocumentPart.Document.Body.InsertBefore(element.CloneNode(true), targetParagraph);
}
}
targetParagraph.Remove();
}
What could be causing this InvalidOperationException during document loading in edit mode?
Are there known issues with OpenXML that might affect compatibility with Office Online Server?
Any insights or similar experiences would be greatly appreciated!