Kentico: Example: Exclude documents based on a column value
In this article you’ll find:
Goal
The purpose of this article is to show you how to exclude certain documents from being written to a csv file based on some database column value. For the purpose of this tutorial, it will be assumed that you have inherited the Hawksearch.Kentico.Xperience.CMS.Services.Indexing.Exporter class and want to extend its behavior instead of writing your own Exporter from scratch.
Prerequisites
Created custom exporter - Kentico: Creating your own File Exporter
Steps to excluding documents from being indexed
Override the AddData method and implement your custom logic. In the following snippet we will exclude all documents which have an “EventEndDate” column name with a value that is in the past, meaning that the event has already passed.
public override IList<DataRetrievingModel> AddData(IList<DataRetrievingModel> data) { var baseData = base.AddData(data); foreach (var baseDataModel in baseData) { if (baseDataModel.Key == "Content") //Exclude only content items. { for (int i = baseDataModel.FeedData.Count - 1; i >= 0; i--) { var columnValue = baseDataModel.FeedData[i]["EventEndDate"]; if (columnValue != null) { var columnDate = DateTime.Parse(columnValue.ToString()); if (columnDate < DateTime.Now) //Date is in the past. { baseDataModel.FeedData.RemoveAt(i); //Remove from content items. } } } } } return baseData; }
That’s it. Now the exporter will exclude documents which have an event end date in the past.