Index RelatedData and RelatedMedia in Sitefinity

Recently I was working on a few projects with Progress Sitefinity CMS where we needed to index a related data field. This can be achieved fairly easy with a custom inbound pipe. But then the client asked us to index more related data fields. Instead to add code to index every related data field that we needed I decided to write something more generic that will allow the client to add the related data fields from the backend using additional fields of the search index. As Sitefinity won’t allow us to use dot, slash, underscore or other special characters for the field name I needed another approach. The easiest way was to use a simple convention for naming the field and with Regex to split it and map to the real fields. You can check the code in the following Gist:

The most important part is the last line:
private Regex relatedDataRegex = new Regex(@"(?<=RelatedData)(\w+)(?>Field)(\w+)", RegexOptions.Compiled);

This is the Regex which I used to map the additional field to the real related data field. It looks for the following format: “RelatedData{FieldName}Field{RelatedFieldProperty}, for ex. "RelatedDataCountryFieldTitle" will index the Title of the related Country field.

Limitation of this approach is that nested related fields won’t work, but the code could be easily extended to support it.

Finally – we need to register the pipe for all dynamic content modules (or only for that you need to). This can be done on “Bootstrapper_Initialized” event in Global.asax.cs: