Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Customize retention policies

Describes how to change the date for a specific submission and how to change the default values through code.

📘

Note

Optimizely Forms is only supported by MVC-based websites and HTML5-compliant browsers.

Editors can set a retention policy in the user interface, controlling how long finalized form submissions and partially submitted form data should be stored.

Use actor to change the expired date of submission

By default, the retention policy of a form applies to all its submissions. You can write a custom actor to change the date for a specific submission. In this example, you create a custom actor to check if a submission comes from an internal or external visitor. If it comes from an internal user, the expired date is set to the previous day, so the submission is deleted right after the Submission Cleanup scheduled job is run.

  1. Create a class that implements the PostSubmissionActorBase and ISyncOrderedSubmissionActor classes.
  2. Implement the class as below:
public class CheckInternalSubmissionActor: PostSubmissionActorBase, ISyncOrderedSubmissionActor {
  public int Order => 500;
  public override object Run(object input) {
    //get client ip
    var geoDataSource = new GeoVisitorDataSource();
    var geoData = geoDataSource.GetGeoDataAsync().Result;
    //get local ip address
    var localIPList = new List<string>() {
      "192.168.1.1"
    };
    //check if submission comes from internal address, if yes then set expired date to 
    previous day
    if (localIPList.Contains(geoData.ip)) {
      var item = SubmissionData;
      item.Data[Constants.SYSTEMCOLUMN_ExpiredDate] = DateTime.UtcNow.AddDays(-1);
    }
    return null;
  }
}​

📘

Note

ExpiredDate should be stored as UTC time format, otherwise it may cause some unexpected issues.

Change the default value of retention policies

Optimizely Forms uses SubmissionRetentionService to get the default value for the retention periods of partial and finalized submissions. You can inherit that class and override the method to get a custom default value for submission retention periods. Example:

[ServiceConfiguration(typeof(SubmissionRetentionService))]
public class CustomSubmissionRetentionService : SubmissionRetentionService {
  /// <summary>
  /// Get default retention period.
  /// <returns>  
  /// A tuple contains 2 items:
  /// Item1: Default retention period (days) value of partial submission.
  /// Item2: Default retention period (days) value of finalized submission.
  /// </returns>
  /// </summary>
  public override Tuple<int, int> GetDefaultRetentionPeriod() {
    return Tuple.Create(20, 60);
  }
}

Then you need to go to DependencyResolverInitialization to register that service:

public void ConfigureContainer(ServiceConfigurationContext context) {
  //Implementations for custom interfaces can be registered here.
  context.ConfigurationComplete += (o, e) => {
    //Register custom implementations that should be used in favour of the default 
    implementations
    context.Services.AddTransient<IContentRenderer, ErrorHandlingContentRenderer>() 
    .AddTransient<ContentAreaRenderer, AlloyContentAreaRenderer>()
    .AddTransient<SubmissionRetentionService, CustomSubmissionRetentionService>(); 
  };
}