Sitecore – Data at the root level is invalid. Line 1, position 1.

I’ve encountered this error message on various Sitecore implementations predominantly while publishing. It’s painful as its fatal to publishing process and Content Editors get uppity when they see the stack trace in the Publishing window.

ERROR Catching Exception in Global.asax_Application_Error
Exception: System.Web.HttpUnhandledException
Message: Exception of type 'System.Web.HttpUnhandledException' was thrown.
Source: System.Web
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Nested Exception

Exception: System.Reflection.TargetInvocationException
Message: Exception has been thrown by the target of an invocation.
Source: mscorlib
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Sitecore.Reflection.ReflectionUtil.InvokeMethod(MethodInfo method, Object[] parameters, Object obj)
at Sitecore.Shell.Applications.ContentManager.ContentEditorPage.OnPreRender(EventArgs e)
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Nested Exception

Exception: System.Xml.XmlException
Message: Data at the root level is invalid. Line 1, position 1.
Source: System.Xml
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XDocument.Parse(String text, LoadOptions options)
at Sitecore.Data.Fields.PagePreviewField.ParseValue(String value)
at Sitecore.Data.Fields.PagePreviewField..ctor(Field innerField)
at Sitecore.ApplicationCenter.PagePreview.PagePreviewManager.GetPreviews(Item item)
at Sitecore.Shell.Framework.Commands.ContentEditor.PagePreviews.GetHeader(CommandContext context, String header)
at Sitecore.Web.UI.WebControls.Ribbons.Ribbon.FillParamsFromCommand(CommandContext commandContext, RibbonCommandParams ribbonCommandParams)
at Sitecore.Web.UI.WebControls.Ribbons.Ribbon.GetCommandParameters(Item controlItem, CommandContext commandContext)
at Sitecore.Web.UI.WebControls.Ribbons.Ribbon.RenderLargeButton(HtmlTextWriter output, Item button, CommandContext commandContext)
at Sitecore.Web.UI.WebControls.Ribbons.Ribbon.RenderButton(HtmlTextWriter output, Item button, CommandContext commandContext)
at Sitecore.Web.UI.WebControls.Ribbons.Ribbon.RenderChunk(HtmlTextWriter output, Item chunk, CommandContext commandContext)
at Sitecore.Web.UI.WebControls.Ribbons.Ribbon.RenderChunk(HtmlTextWriter output, Item chunk, CommandContext commandContext, Boolean isContextual, String id)
at Sitecore.Web.UI.WebControls.Ribbons.Ribbon.RenderChunk(HtmlTextWriter output, Item chunk, CommandContext commandContext, Boolean isContextual)
at Sitecore.Web.UI.WebControls.Ribbons.Ribbon.RenderChunks(HtmlTextWriter output, Item strip, CommandContext commandContext, Boolean isContextual)
at Sitecore.Web.UI.WebControls.Ribbons.Ribbon.RenderStrips(HtmlTextWriter output, Item ribbon, Boolean isContextual, ListString visibleStripList)
at Sitecore.Web.UI.WebControls.Ribbons.Ribbon.RenderStrips(HtmlTextWriter output, Item defaultRibbon, Item contextualRibbon, ListString visibleStripList)
at Sitecore.Web.UI.WebControls.Ribbons.Ribbon.Render(HtmlTextWriter output)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at Sitecore.Web.HtmlUtil.RenderControl(Control ctl)
at Sitecore.Shell.Applications.ContentManager.ContentEditorForm.UpdateRibbon(Item folder, Boolean isCurrentItemChanged, Boolean showEditor)
at Sitecore.Shell.Applications.ContentManager.ContentEditorForm.Update()
at Sitecore.Shell.Applications.ContentManager.ContentEditorForm.OnPreRendered(EventArgs e)

Cause of the problem

From the numerous times I’ve had this problem its due to one or more Items having invalid or inaccurate data in one or more of their Fields. For example;

  • Invalid XML in the rendering fields
  • Plain text in a General Link field
  • A field’s raw data being an incorrect structure
  • Problematic character in a field (á, ¿, etc)
  • Preview field referencing a non-existent version of the Item

The latter happened most recently and definitely the most painful to identify.

Identifying the source

The first step is to identify which item(s) are causing the fatal error to the publish. Sitecore logging does record the error in both the logs and the publishing logs but neither state which item it failed on. Awesome. If you have a vague idea which item(s) are the problem then knowing which to check shouldn’t be so bad. If not. The hunt is on.

Chances are you don’t have time to write a Sitecore Powershell script to traverse the tree and report which item failed to publish (I may actually get around to this one day). The most effective, but brute force, way I use is divide and conquer the tree.

Smart publish each item under the Sitecore root node in turn (Content, Layout, Templates, etc) not forgetting to check subitems. When the error occurs during publish repeat the steps for the Item you just published children. Eventually you should narrow it down to a folder or group items.

Now to find the individual item. In my most recent case the Content Editor failed to load the Item when clicking on it in the tree. A dead give away. More on why it didn’t open later. However, more often than not your Item will open in the Content Editor and you will need to try to identify the raw value in the field of the items that is causing a problem.

That can be a pretty painful task and not an option for me when the Item won’t even open. How can you address the problem if you can’t access the item?

Sitecore’s DB Browser

Sitecore has an admin page out of the box that effectively lets you browse the content tree and see the values as they’re stored in the database. Think of it as a really stripped back version of the Content Editor.

Sitecore admin dbbrowser

Access the database browser via and login with an admin account.

As the dbbrowser is bare bones, feature errors and UI exceptions are kept to a minimum. So in my scenario I could open my problematic item. It also shows the fields in a manner that’s easier to compare, showing the raw data, and flipping between items is almost instantaneous. Making it perfect tool to resolve problems with field data.

Resolving the problem

As mentioned earlier your problem is likely due to special characters, invalid xml and/or invalid data in fields. You should be able to address the first two quite easily. The last one may be more tricky as some Sitecore fields require the raw data to have a particular structure. I recommend comparing the values to values of an item that does publish.

I have read a few posts saying that this exception can be caused by broken links. So use the Broken Links Manager to see if these items reference deleted items. It hasn’t been the source for my scenarios but worth investigating.

Now it may not be any of those above problems, like in my case. So I recommend alternating the view between a publishable item and the unpublishable one. As you browse the fields you should be able to spot differences. Address these differences and publish again.

In my case, the unpublishable Item had the value ‘3’ in the Preview standard field. All others did not. Turns out the Preview field was referencing a version of the Item that did not exist. Removing that value allowed me to open the Item in the Content Editor as well as address the publishing issue.

Not hundred percent certain how the Preview field is used. An educated guess is that it outlines which version of the Item should be opened when previewing. Regardless problem solved with no side-effects.

And that’s it

Hopefully this post has gone some way to helping some of you out when you encounter this issue especially since the log message gives little to go on.

I’m back from my little blogging hiatus so expect the usual regular posts from me.

For more on the massively helpful admin pages Sitecore checkout this great post by Nikola Gotsev.

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s