GetPropertyValue 的 Umbraco 7 自定义字段索引问题

Umbraco 7 Custom Field Indexing issues with GetPropertyValue

RTE 属性的自定义索引字段存在问题(其他 属性 类型似乎工作正常)。

版本:7.3.6

问题摘要:当尝试访问附加到事件 GatheringNodeData 的 OnApplicationStarted 上的 RTE 属性时,抛出错误:

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  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.RteMacroRenderingValueConverter.ConvertDataToSource(PublishedPropertyType propertyType, Object source, Boolean preview)
       at Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedProperty.<.ctor>b__0()
       at System.Lazy`1.CreateValue()
       at System.Lazy`1.LazyInitValue()
       at Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedProperty.<.ctor>b__1()
       at System.Lazy`1.CreateValue()
       at System.Lazy`1.LazyInitValue()
       at CreativeJar.Wickes.BrandingPortal.CMS.Handlers.OnApplicationStart.GatheringNodeDataHandler(Object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper) in D:\CodeChronicles\CreativeJar\Branding Portal\Trunk\Code\Wickes.BrandingPortal.CMS\Handlers\OnApplicationStart.cs:line 64
       at CreativeJar.Wickes.BrandingPortal.CMS.Handlers.OnApplicationStart.<>c__DisplayClass2_0.<OnApplicationStarted>b__0(Object sender, IndexingNodeDataEventArgs e) in D:\CodeChronicles\CreativeJar\Branding Portal\Trunk\Code\Wickes.BrandingPortal.CMS\Handlers\OnApplicationStart.cs:line 35
       at Examine.Providers.BaseIndexProvider.OnGatheringNodeData(IndexingNodeDataEventArgs e) in x:\Projects\Examine\Examine\Projects\Examine\Providers\BaseIndexProvider.cs:line 190
       at UmbracoExamine.UmbracoContentIndexer.OnGatheringNodeData(IndexingNodeDataEventArgs e)
       at Examine.LuceneEngine.Providers.LuceneIndexer.GetDataToIndex(XElement node, String type) in x:\Projects\Examine\Examine\Projects\Examine\LuceneEngine\Providers\LuceneIndexer.cs:line 1113
       at Examine.LuceneEngine.Providers.LuceneIndexer.ProcessIndexQueueItem(IndexOperation op, IndexWriter writer) in x:\Projects\Examine\Examine\Projects\Examine\LuceneEngine\Providers\LuceneIndexer.cs:line 1802
       at Examine.LuceneEngine.Providers.LuceneIndexer.ProcessQueueItem(IndexOperation item, ICollection`1 indexedNodes, IndexWriter writer) in x:\Projects\Examine\Examine\Projects\Examine\LuceneEngine\Providers\LuceneIndexer.cs:line 1580
       at Examine.LuceneEngine.Providers.LuceneIndexer.ForceProcessQueueItems(Boolean block) in x:\Projects\Examine\Examine\Projects\Examine\LuceneEngine\Providers\LuceneIndexer.cs:line 1537
  InnerException: 

联播代码:

var helper = new UmbracoHelper(UmbracoContext.Current);
            ExamineManager.Instance.IndexProviderCollection["ContentIndexer"].GatheringNodeData += (sender, e) => GatheringNodeDataHandler(sender, e, helper);

此处抛出错误:

foreach (var prop in WidgetProperties.Where(prop => widget.HasProperty(prop) && widget.HasValue(prop)))
                {
                    content.Append(widget.GetPropertyValue<string>(prop));
                }

为了找出问题,我尝试直接访问 RTE 属性,结果相同。

var node = helper.TypedContent(e.NodeId);
var test = node.GetPropertyValue("pageSummary");

运行 有属性且有值 returns 真

立即查看 window,我可以在执行 GetProperty("pageSummary") 时看到值,但是尝试访问此 属性 会引发相同的异常,

不幸的是,我无法进入调用,但堆栈跟踪似乎表明在执行时可能存在不可用的依赖项。

有谁知道我是否试图在错误的执行点执行此操作?

我已经尝试过获取原始值的方法(我并不真正关心实际的链接解析等,这似乎是导致问题的原因)但是目前我没有运气。

非常感谢

莱斯

这似乎已经存在一段时间了: (UmbracoContext 在导致错误的调用方法中不可用) http://issues.umbraco.org/issue/U4-5953?preventRedirect=true

这有点奇怪,因为我确信我在 7.3.4 中工作(在更新到 7.3.5 和 7.3.6 之前),因为搜索功能和索引正在更新。

我错误地假设它只是 RTE 属性,但这也无济于事,但它似乎都是基于字符串的。

最后我找到了两种方法来获得我想要的功能,第一种是:

IContentService cs = ApplicationContext.Current.Services.ContentService;

            var node1 = cs.GetById(e.NodeId);
            var widgets1 = node1.Descendants().Where(x => x.ContentType.Alias.Contains("Widget"));
            var content1 = new StringBuilder();

            foreach (var widget in widgets1)
            {
                foreach (var prop in WidgetProperties.Where(prop => widget.HasProperty(prop)))
                {
                    content1.Append(widget.GetValue(prop));
                    content1.Append(" ");
                }
            }

            var result = content1.ToString();

上述方法的问题在于它没有在第一层获得所有后代。

下一个解决方案得到了我预期的结果,但由于其贪婪的性质(没有找到一种只获取我想要的节点的好方法)速度有点慢。由于它是一个小网站,而且只影响内容编辑器,所以它似乎是一个必要的邪恶。

 var node = new Node(e.NodeId);

            var widgets = node.GetDescendantNodes();

            var content = new StringBuilder();
            foreach (var widget in widgets)
            {
                foreach (var prop in WidgetProperties.Where(prop => widget.HasProperty(prop)))
                {
                    content.Append(widget.GetProperty(prop));
                    content.Append(" ");
                }
            }

            e.Fields.Add("pageContent", content.ToString());

我不确定这个问题是否会很快得到解决,因为 Umbraco 团队似乎非常努力地推动网格编辑器(基于节点 components/widgets)。