Different order of columns in the exported CSV files that contain form's data


Description

When forms with non-mandatory fields are submitted empty, no records are saved for them in the FieldData SQL table. Therefore, when an export is performed, if the first submitted record has empty fields, the corresponding columns might be added last in the exported CSV file. This might result in a different column reordering depending on the time of the export.

Solution

To resolve the issue, upgrade your solution to Sitecore XP 10.3.0 or later.

To prevent the issue, you can customize the behavior by modifying the logic of the GenerateFileContent method, as follows:

  1. Create the CsvExportProvider class that inherits the Sitecore.ExperienceForms.Client.Data.CsvExportProvider class and overrides the GenerateFileContent method.

    For example, this line:

    var formData = formEntries.OrderByDescending(item => item.Created);

    Can be replaced with the following line:

    var formData = formEntries;

    Note: The code sample is provided only as a starting point and the logic of the GenerateFileContent method can be modified depending on your needs.

  2. Put the file with the custom class to the bin folder of the website.
  3. Update implementationType of the following service registration with your custom CsvExportProvider class by creating a configuration patch file in the [your_website]\App_Config\Include\zzz folder:
    <register serviceType="Sitecore.ExperienceForms.Data.IExportDataProvider, Sitecore.ExperienceForms" implementationType="Sitecore.ExperienceForms.Client.Data.CsvExportProvider, Sitecore.ExperienceForms.Client" />