/
Process Taxonomies for Media Content

Process Taxonomies for Media Content

Due to Sitefinity's limitations, media content taxonomies cannot be resolved out of the box from the connector and a custom search service should be created for this.

This article provides an example of extending the search service and developing a logic for exporting media content taxonomies.

Prerequisite

Installed Connector - Installing the Connector

Register Custom Search Service

In order to use your custom search service instead of the built-in one you need to register it in the backend.

Please refer to this documentation - Register custom search service

Add Taxonomy Fields to the Index

  1. Go to Sitefinity’s Search indexes backend page and open the the desired index (your-website-domain/Sitefinity/Administration/Search).

  2. In the Advanced section of the search index editor add - Category, Tags

  3. Add a Tag or a Category to one of your Media Content - Documents

Setup search service

In order to add taxonomy fields to the index you need to create a custom search service which inherits the HawkseachService class and overrides the CreateIndex and UpdateIndex methods. Please refer to the code snippet below.

Field names should be exact, as the additional fields are case sensitive.

Process Media Content Taxonomies

using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using Hawksearch.Search; using Telerik.OpenAccess; using Telerik.Sitefinity.Modules.Libraries; using Telerik.Sitefinity.Publishing; using Telerik.Sitefinity.Services.Search.Data; using Telerik.Sitefinity.Taxonomies; using Telerik.Sitefinity.Taxonomies.Model; namespace SitefinityWebApp.Search { public class CustomSearchService : HawksearchService { private const string DocumentType = "Telerik.Sitefinity.Libraries.Model.Document"; public override void UpdateIndex(string name, IEnumerable<IDocument> documents) { var documentList = documents.ToList(); var contentType = string.Empty; var doc = documentList.FirstOrDefault(); if (doc != null && doc.Fields.FirstOrDefault(f => f.Name == "ContentType") != null) { var contentTypeField = doc.Fields.FirstOrDefault(f => f.Name == "ContentType").Value; if (contentTypeField != null) { contentType = contentTypeField.ToString(); } } if (string.Equals(contentType, DocumentType, StringComparison.InvariantCultureIgnoreCase)) { var librariesManager = LibrariesManager.GetManager(); var taxonomyManager = TaxonomyManager.GetManager(); var mediaDocuments = librariesManager.GetDocuments().ToList(); var taxonomies = taxonomyManager.GetTaxa<Taxon>().ToList(); foreach (var document in documentList.Cast<Telerik.Sitefinity.Services.Search.Model.Document>()) { var id = document.Fields.FirstOrDefault(f => f.Name == "Id"); var documentId = Guid.Empty; if (id != null) { documentId = Guid.Parse(id.Value.ToString()); } var properties = TypeDescriptor.GetProperties(mediaDocuments.FirstOrDefault(m => m.Id == documentId)); foreach (PropertyDescriptor property in properties) { if (property != null) { if (property.PropertyType == typeof(TrackedList<Guid>)) { var taxonomyIds = mediaDocuments.FirstOrDefault(m => m.Id == documentId).GetPropertyValue<TrackedList<Guid>>(property.Name); var taxonomyNames = new List<string>(); foreach (var taxonomyId in taxonomyIds) { var taxonName = string.Empty; var taxon = taxonomies.FirstOrDefault(t => t.Id == taxonomyId); if (taxon != null) { taxonName = taxon.Title; } taxonomyNames.Add(taxonName); } if (document.Fields.FirstOrDefault(f => f.Name == property.Name) != null) { document.Fields.FirstOrDefault(f => f.Name == property.Name).Value = taxonomyNames.ToArray(); } } } } } } base.UpdateIndex(name, documentList); } } }

 

Once you implement the code in Visual Studio , build your solution and you will also have to reindex the index you are using from Administrator → Search Indexes → Action → Reindex

 

Find the taxonomies you have added to your Media Content

Now if you Inspect your frontend page you should be able to find the tag or the category fields you have added to your document in the XHR search → results → document fields

 

Related content