Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Chris Norwood 118 posts 617 karma points
    Mar 04, 2020 @ 14:13
    Chris Norwood

    ContentByXPath fails after Umbraco 8 migration

    Hi Umbracians!

    Hoping somebody else has come across the same issue.

    I am in the process of migrating a site from U7 to U8; as part of this, I have downloaded the "experimental" version of the migrations from here:

    And imported the database as per the guide (and then pointed my Umbraco 8.5.3 project at it).

    This seems to have worked - the data is in the format I expect and most of it loads fine, having rewritten the templates etc. to work with Umbraco 8.

    So far so good....

    However, there are some macros which use the ContentAtXPath method to return groups of the same document type - e.g. for the site news, it tries to retrieve most recent news items. The code for that looks like this:

    var xpath = "//newsItem [@isDoc]";
    var newsItemsXpath = Umbraco.ContentAtXPath(xpath);

    Looking at the "newsItemsXpath" variable after this code runs, there is an error when trying to evaluate the enumerator, which is:

    ArgumentException: Could not cast or convert from System.Int64 to System.String[].

    Message: "Error converting value 458 to type 'System.String[]'. Path '', line 1, position 3." string

    This seems to be related to the issue described in the GitHub issue above, except that my understanding here is that ContentAtXpath will retrieve the data from the XML cache.

    The News pages themselves seem to load without an issue, so I'm a bit stumped - I can't find where this data is in Umbraco 8.

    Has anybody else experienced anything like this in V8 using ContentAtXPath?

    (Also - is Xpath still the best way to do this? I could use Linq to get the items, which seems to work, but I'd be concerned about performance as this macro is used in quite a few places).

  • Steve Megson 151 posts 1000 karma points MVP c-trib
    Mar 04, 2020 @ 16:01
    Steve Megson

    Do the logs give you a stack trace for that exception?

    My guess would be that one News page had a bad value which the migration couldn't handle, perhaps a dropdown item which has been removed. Then most News pages would work fine, but ContentAtXPath will find that one bad value and fail.

  • Chris Norwood 118 posts 617 karma points
    Mar 04, 2020 @ 16:59
    Chris Norwood

    Thanks Steve - I did manage to identify one of the nodes that was apparently having this issue by using ContentSingleAtXPath, but the page loaded without an issue so that was doubly confusing.

    Here's the stack trace - I'm not sure it tells you much more than where the error occurred - but I will look in the v7 DB to see if I can find where the prevalue came from initially (it seems to happen regardless of where the macro is executed and it seems to do it on some other document types as well, but all of the document types use Nested Content).

        Newtonsoft.Json.JsonSerializationException: Error converting value 458 to type 'System.String[]'. Path '', line 1, position 3. ---> System.ArgumentException: Could not cast or convert from System.Int64 to System.String[].
       at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType) in /_/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs:line 616
       at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType) in /_/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs:line 572
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) in /_/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:line 982
       --- End of inner exception stack trace ---
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) in /_/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:line 987
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) in /_/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:line 302
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) in /_/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:line 196
       at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) in /_/Src/Newtonsoft.Json/JsonSerializer.cs:line 907
       at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) in /_/Src/Newtonsoft.Json/JsonConvert.cs:line 828
       at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) in /_/Src/Newtonsoft.Json/JsonConvert.cs:line 786
       at Umbraco.Web.PropertyEditors.ValueConverters.FlexibleDropdownPropertyValueConverter.ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, Object source, Boolean preview) in D:\a\1\s\src\Umbraco.Web\PropertyEditors\ValueConverters\FlexibleDropdownPropertyValueConverter.cs:line 24
       at Umbraco.Core.Models.PublishedContent.PublishedPropertyType.ConvertSourceToInter(IPublishedElement owner, Object source, Boolean preview) in D:\a\1\s\src\Umbraco.Core\Models\PublishedContent\PublishedPropertyType.cs:line 208
       at Umbraco.Web.PublishedCache.NuCache.Property.GetInterValue(String culture, String segment) in D:\a\1\s\src\Umbraco.Web\PublishedCache\NuCache\Property.cs:line 192
       at Umbraco.Web.PublishedCache.NuCache.Property.GetXPathValue(String culture, String segment) in D:\a\1\s\src\Umbraco.Web\PublishedCache\NuCache\Property.cs:line 232
       at Umbraco.Web.PublishedCache.NuCache.Navigable.NavigableContent.Value(Int32 index) in D:\a\1\s\src\Umbraco.Web\PublishedCache\NuCache\Navigable\NavigableContent.cs:line 74
       at Umbraco.Core.Xml.XPath.NavigableNavigator.MoveToFirstChildProperty() in D:\a\1\s\src\Umbraco.Core\Xml\XPath\NavigableNavigator.cs:line 602
       at Umbraco.Core.Xml.XPath.NavigableNavigator.MoveToFirstChild() in D:\a\1\s\src\Umbraco.Core\Xml\XPath\NavigableNavigator.cs:line 562
       at MS.Internal.Xml.XPath.XPathDescendantIterator.MoveNext()
       at MS.Internal.Xml.XPath.DescendantQuery.Advance()
       at MS.Internal.Xml.XPath.FilterQuery.Advance()
       at MS.Internal.Xml.XPath.XPathSelectionIterator.MoveNext()
       at Umbraco.Web.PublishedCache.NuCache.ContentCache.<GetByXPath>d__31.MoveNext() in D:\a\1\s\src\Umbraco.Web\PublishedCache\NuCache\ContentCache.cs:line 358
       at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
       at System.Linq.Enumerable.<TakeIterator>d__25`1.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at ASP._Page_Views_MacroPartials_RenderLatestNews_cshtml.Execute() in []\Views\MacroPartials\RenderLatestNews.cshtml:line 40
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
       at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
       at Umbraco.Web.Mvc.ProfilingView.Render(ViewContext viewContext, TextWriter writer) in D:\a\1\s\src\Umbraco.Web\Mvc\ProfilingView.cs:line 25
       at Umbraco.Web.Mvc.ControllerExtensions.RenderViewResultAsString(ControllerBase controller, ViewResultBase viewResult) in D:\a\1\s\src\Umbraco.Web\Mvc\ControllerExtensions.cs:line 74
       at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, IPublishedContent content) in D:\a\1\s\src\Umbraco.Web\Macros\PartialViewMacroEngine.cs:line 98
       at Umbraco.Web.Macros.MacroRenderer.<>c__DisplayClass17_0.<ExecuteMacroOfType>b__0() in D:\a\1\s\src\Umbraco.Web\Macros\MacroRenderer.cs:line 335
       at Umbraco.Web.Macros.MacroRenderer.ExecuteProfileMacroWithErrorWrapper(MacroModel macro, String msgIn, Func`1 getMacroContent, Func`1 msgErr) in D:\a\1\s\src\Umbraco.Web\Macros\MacroRenderer.cs:line 306
       at Umbraco.Web.Macros.MacroRenderer.ExecuteMacroWithErrorWrapper(MacroModel macro, String msgIn, String msgOut, Func`1 getMacroContent, Func`1 msgErr) in D:\a\1\s\src\Umbraco.Web\Macros\MacroRenderer.cs:line 265
       at Umbraco.Web.Macros.MacroRenderer.ExecuteMacroOfType(MacroModel model, IPublishedContent content) in D:\a\1\s\src\Umbraco.Web\Macros\MacroRenderer.cs:line 332
       at Umbraco.Web.Macros.MacroRenderer.Render(MacroModel macro, IPublishedContent content, IDictionary pageElements) in D:\a\1\s\src\Umbraco.Web\Macros\MacroRenderer.cs:line 238
       at Umbraco.Web.Macros.MacroRenderer.Render(String macroAlias, IPublishedContent content, IDictionary`2 macroParams) in D:\a\1\s\src\Umbraco.Web\Macros\MacroRenderer.cs:line 209
       at Umbraco.Web.UmbracoComponentRenderer.RenderMacro(String alias, IDictionary`2 parameters, IPublishedContent content) in D:\a\1\s\src\Umbraco.Web\UmbracoComponentRenderer.cs:line 116
       at Umbraco.Web.UmbracoComponentRenderer.RenderMacro(Int32 contentId, String alias, IDictionary`2 parameters) in D:\a\1\s\src\Umbraco.Web\UmbracoComponentRenderer.cs:line 100
       at Umbraco.Web.UmbracoHelper.RenderMacro(String alias, IDictionary`2 parameters) in D:\a\1\s\src\Umbraco.Web\UmbracoHelper.cs:line 171
       at ASP._Page_Views_Partials_grid_editors_Macro_cshtml.Execute() in \Views\Partials\grid\editors\Macro.cshtml:line 15
  • Steve Megson 151 posts 1000 karma points MVP c-trib
    Mar 04, 2020 @ 17:28
    Steve Megson

    I think the problem value might be a dropdown property rather than nested content, and any call to ContentAtXPath will fail if it has to read past the problem content.

    Hopefully the migration process should have logged warnings saying "Could not find PropertyData {PropertyDataId} value '{PropertyValue}' in the datatype configuration: {Values}", which will give you the IDs of the offending rows in umbracoPropertyData. Saving and publishing the affected content should remove the unrecognised value and fix the cache.

    I guess the real solution is for FlexibleDropdownPropertyValueConverter to do more error checking, so it just returns no value if the database/cache contains something it can't make sense of.

  • Chris Norwood 118 posts 617 karma points
    Mar 06, 2020 @ 12:03
    Chris Norwood

    Thanks Steve - deleted my previous comment as it wasn't helpful, because the issue had nothing to do with the News Item document type.

    You were exactly correct - the error was due to a dropdown pre-value that had (at some point in the distant past) been deleted, so the migration process couldn't find the actual value, and had inserted the ID of the prevalue (458) into the textValue field in UmbracoProperty data, and the Xpath method was failing when it tried to read past it, so republishing didn't help because the issue had nothing to do with the node that looked like it caused the exception.

    I was able to identify this from an old copy of the database (before the prevalue was deleted/changed) and update the property data values to the new required value in the V8 database, and it all seems happy now.

  • Ebin 9 posts 78 karma points
    Mar 09, 2020 @ 12:07


    Umbraco.ContentAtXPath(xpath); is replaced by

    var globalArea = Umbraco.ContentQuery.ContentAtXPath("//global").FirstOrDefault();

    //global is Root Document type

Please Sign in or register to post replies

Write your reply to: