Mike Masey
06/05/2024, 12:21 PMUmbracoContextFactory
worked, but I'm using it inside some custom Examine Indexing to manipulate certain indexed data, such as a searchable path etc.
One thing I'm trying to do I get the name of a parent page as add it to the index of the current page for use as a "category" name . When using the IPublishedContentCache
provided by the UmbracoContextFactory
, the Name
value is always empty thought.
Am I just doing this wrong, or should this be returning the correct value
Here's a snippet of the code in question, it may look a little odd where i'm playing around with things, but the focus is on just getting the actual data from the parent page.
// I've already used the umbracoContextFactory to get the `content` value that is passed in here
// I've tried doing `content.Parent` but that doesn't work either
internal void AddCategory(IndexingItemEventArgs e, IPublishedContent content)
{
if (content.ContentType.Alias != ArticlePage.ModelTypeAlias || content.Parent == null) return;
using var umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext();
var contentCache = umbracoContextReference.UmbracoContext.Content;
if (contentCache == null)
{
throw new InvalidOperationException("Could not acquire content cache");
}
var parent = contentCache.GetById(content.Parent.Id);
var parentName = parent?.Name;
if (string.IsNullOrEmpty(parentName)) return;
var updateValues = e.ValueSet.Values.ToDictionary(x => x.Key, x => x.Value.ToList());
updateValues.Add(ExamineConstants.Articles.Category, [parentName]);
e.SetValues(updateValues.ToDictionary(x => x.Key, x => (IEnumerable<object>)x.Value));
}
Any help would be appreciatedJemayn
06/05/2024, 1:01 PMparent
? And even further whether content.Parent.Id
actually has a value? 🙂
Your approach is very similar to what I've done before without problemsMike Masey
06/05/2024, 1:03 PMJemayn
06/05/2024, 1:05 PMMike Masey
06/05/2024, 1:06 PMMike Masey
06/05/2024, 2:35 PMMike Masey
06/05/2024, 2:48 PMMike Masey
06/05/2024, 3:23 PMAllow vary by culture
enabled on the pages. Turning this off fixes the issue, so I'm guessing somewhere in my code i'm not accounting for themSander L
06/05/2024, 6:49 PM