Modifying Examine field value on save

I have some Taxonomy that I want to modify when a document is being saved/published.
In backoffice it looks like this.

This is how it looks in the index right now for the “tags” field:
[“Udgivelser”,“Sanglege”,“Rytme/puls/bodypercussion”,“Opvarmning/Stemmetræning/Energizere”,“Rekvisitsange/instrumenter”,“Noder”]

And I would like to look like this:
[“Udgivelser”,“Sanglege”,“Rytme”,“puls”,“bodypercussion”,“Opvarmning”,“Stemmetræning”,“Energizere”,“Rekvisitsange”,“instrumenter”,“Noder”]

I know how to use notifications to hook into the saving/publishing so that not the issue and I also have made the split of the original data.

I basically just need to figure out how to update the field in the index.
Can anyone give me a few pointers ?

This is how my code looks for now:

public void Handle(ContentPublishingNotification notification)
		{
			if (!_examineManager.TryGetIndex(Constants.UmbracoIndexes.ExternalIndexName, out IIndex index))
			{
				throw new InvalidOperationException($"No index found by name{Constants.UmbracoIndexes.ExternalIndexName}");
			}

			if (notification.PublishedEntities.Any())
			{
				foreach (var item in notification.PublishedEntities)
				{
					var node = _umbracoHelper.Content(item.Id);
					if (node != null && node.ContentType.CompositionAliases.Contains("taxonomiPageComposition"))
					{
						var tags = node.Value<List<string>>("tags") ?? new List<string>();
						var splitTags = tags.SelectMany(tag => tag.Split('/', StringSplitOptions.RemoveEmptyEntries)).ToList();

						// How do I find the document in the index and update the field named tags ?

					}
				}
			}
			
		}

Hi Sebastian,

With what you are trying to do the saving/published events are not the right place to intercept the behaviour. After the Saving/Saved/Publish events run the will trigger Examine to update but Examine has it’s own events that you can hook into.

Typically you would do this using an Umbraco Notification handler that might look something like this:

public class TransformingExamineValuesHandler : INotificationHandler<UmbracoApplicatoinStartingNotification>

In the Handle method implementation you would get the relevant index (typically the internal one), convert it to the BaseIndexProvider and then assign an event handler to the TransformingIndexValues event.

if (_examineManager.TryGetIndex(Umbraco.Cms.Core.Constants.UmbracoIndexes.ExternalIndexName, out var index))
{
    ((BaseIndexProvider)index).TransformingIndexValues += (_, e) =>
    {

In side the event handler you can extract the data, update it and, then re-add it before it gets written to the index.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.