Skip to content

Instantly share code, notes, and snippets.

@ProNotion
Created May 23, 2024 13:19
Show Gist options
  • Save ProNotion/c1377570afa1a2ebe43fe27799c2ace9 to your computer and use it in GitHub Desktop.
Save ProNotion/c1377570afa1a2ebe43fe27799c2ace9 to your computer and use it in GitHub Desktop.
Umbraco v12 - Add Custom field to Examine Index
public class MyServiceComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.AddNotificationHandler<UmbracoApplicationStartedNotification, UmbracoStartedNotificationHandler>();
}
}
public class UmbracoStartedNotificationHandler : INotificationHandler<UmbracoApplicationStartedNotification>
{
private readonly IExamineManager _examineManager;
private readonly IUmbracoContextFactory _umbracoContextFactory;
public UmbracoStartedNotificationHandler(IExamineManager examineManager, IUmbracoContextFactory umbracoContextFactory)
{
_examineManager = examineManager;
_umbracoContextFactory = umbracoContextFactory;
}
/// <summary>
/// Handles the <see cref="UmbracoApplicationStartedNotification"/> event by adding an event handler for the
/// TransformingIndexValues event of an index provider.
/// </summary>
/// <param name="notification">
/// The <see cref="UmbracoApplicationStartedNotification"/> object that contains the event data.
/// </param>
public void Handle(UmbracoApplicationStartedNotification notification)
{
if (!_examineManager.TryGetIndex(UmbracoConstants.UmbracoIndexes.ExternalIndexName, out IIndex index))
throw new InvalidOperationException(
$"No index found with the name {UmbracoConstants.UmbracoIndexes.ExternalIndexName}");
if (index is not BaseIndexProvider indexProvider)
throw new InvalidOperationException(
$"{UmbracoConstants.UmbracoIndexes.ExternalIndexName} does not inherit from {nameof(BaseIndexProvider)}");
// Add an event handler for the TransformingIndexValues event
indexProvider.TransformingIndexValues += IndexProviderOnTransformingIndexValues;
}
/// <summary>
/// Event handler for the TransformingIndexValues event of an index provider.
/// </summary>
/// <param name="sender">The object that triggered the event.</param>
/// <param name="e">An instance of the <see cref="IndexingItemEventArgs"/> class that contains the event data.</param>
private void IndexProviderOnTransformingIndexValues(object? sender, IndexingItemEventArgs e)
{
// If we are not dealing with content we can leave here, there is nothing to do
if (e.ValueSet.Category != IndexTypes.Content) return;
if (!e.ValueSet.Values.TryGetValue("__Key", out var key) || !Guid.TryParse(key[0]?.ToString(), out var contentKey))
{
return;
}
using var contextReference = _umbracoContextFactory.EnsureUmbracoContext();
// Get the published content
var publishedContent = contextReference?.UmbracoContext?.Content?.GetById(contentKey);
// No content item so exit here as nothing further to do
if (publishedContent == null) return;
// Get the key for the root node
var siteKey = publishedContent.AncestorOrSelf(1).Key;
// Create a new dictionary including the existing values and the new siteId field
var values =
e.ValueSet.Values.ToDictionary(kvp => kvp.Key, kvp => (IEnumerable<object>) kvp.Value);
values["siteKey"] = new List<object> { siteKey };
// Set the values with the addition of the siteKey
e.SetValues(values);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment