0

I have a request that I need to use Sitecron to compile the form's data into single CSV file. How can I get access to all the form's saved data in code behind (not WFFM)? I'm using Sitecore 9.1

2

Sitecore Forms already have the functionality to export data.

You can use bellow class to export data:

  [ServicesController("Forms.ExportData")]
  [SitecoreAuthorize(Roles = "sitecore\\Forms Editor")]
  [SetFormMode]
  public class ExportDataController : ServicesApiController
  {
    private readonly IExportDataProvider _exportDataProvider;

    public ExportDataController(IExportDataProvider exportDataProvider)
    {
      Assert.ArgumentNotNull((object) exportDataProvider, nameof (exportDataProvider));
      this._exportDataProvider = exportDataProvider;
    }

    [HttpGet]
    [ActionName("DefaultAction")]
    [ValidateModelState]
    public HttpResponseMessage ExportToCsv(
      [FromUri] ExportDataParameters exportDataParameters)
    {
      HttpResponseMessage httpResponseMessage1 = new HttpResponseMessage();
      HttpResponseMessage httpResponseMessage2;
      try
      {
        if (exportDataParameters == null)
        {
          httpResponseMessage1.StatusCode = HttpStatusCode.BadRequest;
          httpResponseMessage1.ReasonPhrase = Translate.Text("Invalid or missing parameters.");
        }
        else
        {
          ExportDataResult exportDataResult = this._exportDataProvider.Export(exportDataParameters.FormId, exportDataParameters.StartDate, exportDataParameters.EndDate);
          string str;
          if (exportDataResult == null)
          {
            httpResponseMessage1.StatusCode = HttpStatusCode.NoContent;
            str = "nodata";
          }
          else if (string.IsNullOrEmpty(exportDataResult.Content))
          {
            httpResponseMessage1.StatusCode = HttpStatusCode.NoContent;
            httpResponseMessage1.ReasonPhrase = Translate.Text("The date range is invalid. Please select a date range that is within the range of the list.");
            str = "nodatainrange";
          }
          else
          {
            httpResponseMessage1.Content = (HttpContent) new StringContent(exportDataResult.Content);
            httpResponseMessage1.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            httpResponseMessage1.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
              FileName = exportDataResult.FileName
            };
            str = exportDataParameters.Token;
          }
          if (HttpContext.Current != null)
            HttpContext.Current.Response.AppendCookie(new HttpCookie("fileDownloadToken" + exportDataParameters.Token, str));
        }
        httpResponseMessage2 = httpResponseMessage1;
        httpResponseMessage1 = (HttpResponseMessage) null;
      }
      finally
      {
        httpResponseMessage1?.Dispose();
      }
      return httpResponseMessage2;
    }
  }

If you want to use your own ExportDataProvider you need to register your own export provider like this one :

    <register serviceType="Sitecore.ExperienceForms.Data.IExportDataProvider, Sitecore.ExperienceForms" implementationType="Sitecore.ExperienceForms.Client.Data.CsvExportProvider, Sitecore.ExperienceForms.Client" />
3
  • Thank you @VladIobagiu for a very detail answear, this is what i need. But can you show me how to get a specific form and export it by name. Because i think sitecore only store the form's id in the database, so for example : export Form "A". Is there a collection of form i can get the id from or something like that ? – Thanh Nguyen Jun 26 '20 at 7:02
  • you need to get the form by path and then search by id in the form database – Vlad Iobagiu Jun 26 '20 at 7:44
  • okay thank you very much – Thanh Nguyen Jun 26 '20 at 8:20
0

If you want to build a report with Sitecore PowerShell Extensions, you can start with this:

$formId = "{FF4009B6-CA95-440E-884A-984496C65E32}"
$startDate = [datetime]::Now.AddDays(-30)
$endDate = [datetime]::Now
$serviceType = [Sitecore.ExperienceForms.Data.IExportDataProvider]
$service = [Sitecore.DependencyInjection.ServiceLocator]::ServiceProvider.GetService($serviceType) -as $serviceType
$exportedData = $service.Export($formId, $startDate.ToUniversalTime(), $endDate.ToUniversalTime())
$reportData = ConvertFrom-Csv -InputObject $exportedData.Content -Delimiter ';'
$reportData | Show-ListView

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.