EnsureUmbracoContext inside an Examine Indexer com...
# help-with-umbraco
m
This may simply be a misunderstanding of how the
UmbracoContextFactory
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.
Copy code
// 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 appreciated
j
Have you checked if you actually get the
parent
? And even further whether
content.Parent.Id
actually has a value? 🙂 Your approach is very similar to what I've done before without problems
m
Yeah it does get the Parent node / it doesn't return null can I can see the other properties on it, although I'm not sure if any of them have a value either. For context as well, this is on Umbraco 13.3.1 and I have a feeling I saw something about Examin is the latest release so I'll upgrade and see if that fixes it, although probably not
j
I'd also try downgrading.. I've had some weird context issues in 13.3.1 and 13.3.2 that I am still working on trying to reproduce for an issue.. May work on 13.3.0
m
Thanks for the tip, will have a play around
Unfortunately both the upgrade and downgrade didn't work 😦
I think it may be something to do with cultures 🤔
Looks like the source of the issue was due to having
Allow 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 them
s
When you have vary by culture on, you can/need to set the variation context accessor to the right culture
7 Views