Welcome to the navigation

Anim ut magna incididunt reprehenderit ullamco officia eu ad et ex nulla consequat, duis sed in in est cupidatat minim enim laboris consectetur deserunt sit. Anim est aliqua, reprehenderit aliquip eiusmod esse voluptate ut ex laboris ut minim laborum, in veniam, dolore mollit id non in velit officia do dolore

Yeah, this will be replaced... But please enjoy the search!

Episerver Forms Cleanup for GDPR

Categories Tags

NB: This is the super duper simple first version of this "plugin"! GDPR made us all available that we need to maintain any data we collect, which includes form posts. Here's a simple method of automatically deleting any form posts older than X days.

Setup

Add the EpiserverFormsILM setting in web.config

<add key="EpiserverFormsILM" value="60"/>

Add the nuget package System.ValueTuple

Install-Package System.ValueTuple

Code

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using EPiServer.DataAbstraction;
using EPiServer.Forms.Core;
using EPiServer.Forms.Core.Data;
using EPiServer.Forms.Core.Models;
using EPiServer.PlugIn;
using EPiServer.Scheduler;
using EPiServer.ServiceLocation;

// Do whatever you want, www.herlitz.io
namespace Herlitz.Web.Business.AdminJobs
{
    [ScheduledPlugIn(DisplayName = "Episerver Forms Cleanup", Description = "Better GDPR compliance for Episerver forms")]
    public class EpiserverFormsILM : ScheduledJobBase
    {
        private bool _stopSignaled;
        private Injected _formRepository;
        private Injected _formDataRepository;
        private Injected _languageBranchRepository;

        public EpiserverFormsILM()
        {
            IsStoppable = true;
        }

        /// 
        /// Called when a user clicks on Stop for a manually started job, or when ASP.NET shuts down.
        /// 
        public override void Stop()
        {
            _stopSignaled = true;
        }

        /// 
        /// Called when a scheduled job executes
        /// 
        /// A status message to be stored in the database log and visible from admin mode
        public override string Execute()
        {
            //Call OnStatusChanged to periodically notify progress of job for manually started jobs
            OnStatusChanged($"Starting execution of {this.GetType()}");

            if (_stopSignaled)
            {
                return "Stop of job was called";
            }


            var languages = _languageBranchRepository.Service.ListEnabled().Select(x => x.Culture.TwoLetterISOLanguageName).ToList();

            // take all forms in all languages
            var allForms = _formRepository.Service.GetFormsInfo(null);

            List<(string, int, string)> submissionCount = new List<(string, int, string)>();

            foreach (var form in allForms)
            {
                if (_stopSignaled)
                {
                    submissionCount.Add(($"{form.Name}", 0, " The job was stopped"));
                    break;
                }

                foreach (var language in languages)
                {
                    if (_stopSignaled)
                    {
                        break;
                    }


                    try
                    {
                        int episerverFormsILMperiod = -int.Parse(ConfigurationManager.AppSettings["EpiserverFormsILM"]);

                        var result = _formDataRepository.Service.GetSubmissionDataCount(
                         formIden: new FormIdentity(form.FormGuid, language),
                         beginDate: DateTime.MinValue,
                         endDate: DateTime.Now.AddDays(episerverFormsILMperiod),
                         finalizedOnly: true);


                        if (result > 0)
                        {
                            submissionCount.Add((form.Name, result, language));

                            var submissions = _formDataRepository.Service.GetSubmissionData(
                                formIden: new FormIdentity(form.FormGuid, language),
                                beginDate: DateTime.MinValue,
                                endDate: DateTime.Now.AddDays(episerverFormsILMperiod),
                                finalizedOnly: true);

                            _formDataRepository.Service.DeleteSubmissionData(
                                formIden: new FormIdentity(form.FormGuid, language),
                                submissionIds: submissions.Select(x => x.Id).ToArray());
                        }

                    }
                    catch (Exception ex)
                    {
                        // Let's get on with our lives
                        submissionCount.Add((form.Name, 0, $"{language}; {ex.Message}"));
                    }
                }
            }


            // Output string 
            var output = "";

            foreach (var value in submissionCount)
            {
                Console.WriteLine($"{value.Item1}: {value.Item2} - {value.Item3}");
                output = output + $"{value.Item1}: {value.Item2} - {value.Item3}\n
"; } return output; } } }

 

Readme

Configure the scheduled job in the admin gui in episerver, the code will delete any form submissions older than X days and report any errors on doing so.