U4-5953 - 6.2.4 Calling IPublishedContent.GetPropertyValue in GatheringNodeData event handler throws NullReferenceException

Created by Steven Harland 06 Dec 2014, 14:01:57 Updated by David van de Vliet 23 Feb 2018, 11:46:18

Environment:

  • Visual Studio Express 2013 for Web
  • IIS Express
  • ASP.NET MVC 4 project
  • Fresh Umbraco 6.2.4 install using NuGet
  • SQL CE 4

Steps to recreate the issue:

  • Create a new document type with a single property and allow at root
  • Create a node with this document type
  • Add the following class into the project, replacing 1052 with the ID of the node and "test" with the alias of the property:

using Examine; using System; using Umbraco.Core; using Umbraco.Web;

namespace UmbracoTest.Events { public class ExamineEvents : ApplicationEventHandler { private UmbracoHelper Umbraco { get; set; }

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        Umbraco = new UmbracoHelper(UmbracoContext.Current);
        ExamineManager.Instance.IndexProviderCollection["ExternalIndexer"].GatheringNodeData += ExternalIndexer_GatheringNodeData;
    }

    private void ExternalIndexer_GatheringNodeData(object sender, IndexingNodeDataEventArgs e)
    {
        try
        {
            var node = Umbraco.TypedContent(1052);
            var value = node.GetPropertyValue<string>("test");
            e.Fields.Add("customField", value);  // Not reached
        }
        catch (Exception exception)
        {
        }
    }
}

}

  • Publish the node
  • The stack trace of the exception caught will be similar to the following:

at Umbraco.Web.Templates.TemplateUtilities.ParseInternalLinks(String text, Boolean preview) at Umbraco.Web.PropertyEditors.ValueConverters.TextStringValueConverter.ConvertDataToSource(PublishedPropertyType propertyType, Object source, Boolean preview) at Umbraco.Core.Models.PublishedContent.PublishedPropertyType.ConvertDataToSource(Object source, Boolean preview) at Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedProperty.<.ctor>b__0() at System.Lazy1.CreateValue() at System.Lazy1.LazyInitValue() at System.Lazy1.get_Value() at Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedProperty.<.ctor>b__1() at System.Lazy1.CreateValue() at System.Lazy1.LazyInitValue() at System.Lazy1.get_Value() at Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedProperty.get_Value() at Umbraco.Web.PublishedPropertyExtension.GetValue[T](IPublishedContentProperty property, Boolean withDefaultValue, T defaultValue) at Umbraco.Web.PublishedContentExtensions.GetPropertyValue[T](IPublishedContent content, String alias, Boolean recurse, Boolean withDefaultValue, T defaultValue) at Umbraco.Web.PublishedContentExtensions.GetPropertyValue[T](IPublishedContent content, String alias) at UmbracoTest.Events.ExamineEvents.ExternalIndexer_GatheringNodeData(Object sender, IndexingNodeDataEventArgs e) in c:\Users\Steven\Documents\Visual Studio 2013\Projects\UmbracoTest\UmbracoTest\Events\ExamineEvents.cs:line 23

Comments

Marek 20 Feb 2015, 18:17:36

Your code is running without UmbracoContext.

Line 18 from here https://github.com/umbraco/Umbraco-CMS/blob/5b9a98ad6ae9e63322c26f7b162204e34f7fcb54/src/Umbraco.Web/Templates/TemplateUtilities.cs

public static class TemplateUtilities { //TODO: Pass in an Umbraco context!!!!!!!! Don't rely on the singleton so things are more testable internal static string ParseInternalLinks(string text, bool preview)

Methinks it needs moar of these "!!!!!!!!"!


Shannon Deminick 26 Jun 2017, 07:08:48

Closing issue due to inactivity - see blog post for details https://umbraco.com/blog/issue-tracker-cleanup/


Gerty Engrie 25 Aug 2017, 13:50:02

Using version 7.2.1 Situation: a node with a contentpicker property (lets say it's called linkedpage), i also want the linked page document's RTE content to be indexed in the original node.

So, this is what i have:

public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { var helper = new UmbracoHelper(UmbracoContext.Current); ExamineManager.Instance.IndexProviderCollection["GuideIndexer"].GatheringNodeData += (sender, e) => GuideIndexerGatheringNodeData(sender, e, helper); }

    void GuideIndexerGatheringNodeData(object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper)
    {
        var page = helper.TypedContent(e.NodeId);
            if (e.IndexType == IndexTypes.Content && page.HasValue("linkedpage"))
            {
                var content = helper.TypedContent(page.GetPropertyValue<int>("linkedpage")).GetPropertyValue<string>("text")
                e.Fields.Add("text", content);
            }
    }

It all works fine until you want to fetch RTE content, then it throws an error on Umbraco.Web.Templates.TemplateUtilities.ParseInternalLinks(String text, Boolean preview) Solved it atm by doing this:

var content = new Node(page.GetPropertyValue("linkedpage")).Properties["text"].Value;


Shannon Deminick 25 Aug 2017, 14:41:10

Indexing is done on data stored in the database, not data in the published content. You should not use the data in the published content for indexing and instead use the data from the services - but be careful about over querying and N+1 issues.

That said, it would be helpful if you provided the full exception, stack trace, steps to reproduce, etc...


David van de Vliet 23 Feb 2018, 11:46:18

Using version 7.6.8

Having the same issue.

What works for me as a workaround is querying the value indirectly from the property:

var value = content.GetProperty(fieldName)?.DataValue?.ToString();

This is the (ANONymised) stack trace where the call to Indexer_GatheringNodeData is similar to GuideIndexerGatheringNodeData as described above:

System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=umbraco StackTrace: at Umbraco.Web.Templates.TemplateUtilities.ParseInternalLinks(String text, Boolean preview) at Umbraco.Web.PropertyEditors.ValueConverters.TextStringValueConverter.ConvertDataToSource(PublishedPropertyType propertyType, Object source, Boolean preview) at Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedProperty.get_Value() at Umbraco.Web.PublishedPropertyExtension.GetValue[T](IPublishedProperty property, Boolean withDefaultValue, T defaultValue) at Umbraco.Web.PublishedContentExtensions.GetPropertyValue[T](IPublishedContent content, String alias) at ANON.GatherContactpersoonData(IndexingNodeDataEventArgs e, UmbracoHelper helper) in C:\Source\ANON\UmbracoExamineEventHandler.cs:line 41 at ANON.Indexer_GatheringNodeData(Object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper) in C:\Source\ANON\UmbracoExamineEventHandler.cs:line 30 at ANON.UmbracoExamineEventHandler.<>c__DisplayClass1_0.b__0(Object sender, IndexingNodeDataEventArgs e) in C:\Source\ANON\UmbracoExamineEventHandler.cs:line 22 at Examine.Providers.BaseIndexProvider.OnGatheringNodeData(IndexingNodeDataEventArgs e) at UmbracoExamine.UmbracoContentIndexer.OnGatheringNodeData(IndexingNodeDataEventArgs e) at Examine.LuceneEngine.Providers.LuceneIndexer.GetDataToIndex(XElement node, String type) at Examine.LuceneEngine.Providers.LuceneIndexer.ProcessIndexQueueItem(IndexOperation op, IndexWriter writer) at Examine.LuceneEngine.Providers.LuceneIndexer.ProcessQueueItem(IndexOperation item, ICollection`1 indexedNodes, IndexWriter writer) at Examine.LuceneEngine.Providers.LuceneIndexer.ForceProcessQueueItems(Boolean block)


Priority: Normal

Type: Bug

State: Reopened

Assignee:

Difficulty: Normal

Category:

Backwards Compatible: True

Fix Submitted:

Affected versions: 6.2.4, 7.2.1, 7.2.2, 7.2.3

Due in version:

Sprint:

Story Points:

Cycle: