Customizing the built-in Sitefinity Excel export

Motivation:

Every now and then we need to export some content type from Sitefinity in Excel. Sitefinity allows content items exporting for dynamic content types. However, not many people know this can also be extended to include custom data. In this short blog post I will show you how to export the items along with their owner information.

Solution:

Sitefinity uses IoC, and luckily the default exporter is registered. So extending it and replacing it is not difficult at all. 

The interface we need to implement is "IDataItemExporter", however, in the current situation it is easier to extend the class Sitefinity provides, instead of implementing everything on our own. This class is "ExcelExporter" in the " Telerik.Sitefinity.Data.Utilities.Exporters" namespace.

Step 1: 
Is of course to do the Global.asax.cs bootstrapped event handling as we normally do:

Step 2:
Is to actually implement the class:

Not too much here. The built-in Sitefinity Exporter has a few methods and the ones we are interested in are:

CreateHeadersCells - creates the headers (e.g. the columns).
CreateContentRow -creates a row for each data item.

Build the project and run it.
If you now go to the Sitefinity backend => Administration => Module builder and click "Actions" => "Export content" for any module - you will also see the owner of the item exported in the excel.

Things to note:

  1. This will run for any content type. But we want this to work for all of them anyway, so we are just making sure that the content type implements IOwnership interface. It means it has Owner property. This by the way should be true for all dynamic content items, but it is still a good thing to ensure your code has everything it needs (in this case - the item has owner)
  2. If you want to implement this for a specific type - you can do a type check and execute the base functionality for types you are not interested in
  3. It is absolutely critical headers and rows to have same number of columns (remember all those excels and CSVs where thins just don't match :)