Skip to content

Instantly share code, notes, and snippets.

@alasvant
Last active June 18, 2022 11:55
Show Gist options
  • Save alasvant/b114f32c0f991efbbe0a628a6fe6ddee to your computer and use it in GitHub Desktop.
Save alasvant/b114f32c0f991efbbe0a628a6fe6ddee to your computer and use it in GitHub Desktop.
Optimizely World blog post: Custom placeholders in Optimizely Forms submission emails. https://world.optimizely.com/blogs/Antti-Alasvuo/Dates/2022/6/custom-place-holders-in-optimizely-forms-submission-emails/
using System;
using System.Collections.Generic;
using EPiServer.Core;
using EPiServer.Forms;
using EPiServer.Forms.Core.Models;
namespace AlloyWithFind.Features.FormsCustomPlaceholder
{
/// <summary>
/// Extension methods for <see cref="Submission.Data"/>.
/// </summary>
public static class FormSubmissionDataExtensions
{
/// <summary>
/// Tries to get the language code.
/// </summary>
/// <param name="data"><see cref="Submission.Data"/> dictionaty to be extended.</param>
/// <param name="languageCode">Language code.</param>
/// <returns>True if the language code was in the data and the value is not null or empty or just whitespace characters, otherwise false.</returns>
public static bool TryGetLanguage(this IDictionary<string, object> data, out string languageCode)
{
languageCode = null;
// language type is string
if (data != null && data.TryGetValue(Constants.SYSTEMCOLUMN_Language, out object language) && language is string lang && !string.IsNullOrWhiteSpace(lang))
{
languageCode = lang;
return true;
}
return false;
}
/// <summary>
/// Tries to get the submit time.
/// </summary>
/// <param name="data"><see cref="Submission.Data"/> dictionaty to be extended.</param>
/// <param name="submitted">DateTime when the data was submitted.</param>
/// <returns>True if there was a submit time otherwise false.</returns>
public static bool TryGetSubmitTime(this IDictionary<string, object> data, out DateTime submitted)
{
submitted = DateTime.MinValue;
// submit time type is DateTime
if (data != null && data.TryGetValue(Constants.SYSTEMCOLUMN_SubmitTime, out object submitTime) && submitTime is DateTime timestamp)
{
submitted = timestamp;
return true;
}
return false;
}
/// <summary>
/// Tries to get the username.
/// </summary>
/// <param name="data"><see cref="Submission.Data"/> dictionaty to be extended.</param>
/// <param name="username">Username for the submitted data (Note! This value can be null).</param>
/// <returns>True if the data contained the username otherwise false.</returns>
public static bool TryGetSubmitUser(this IDictionary<string, object> data, out string username)
{
username = null;
// submit user type is string
if (data != null && data.TryGetValue(Constants.SYSTEMCOLUMN_SubmitUser, out object submitUser) && submitUser is string user)
{
username = user;
return true;
}
return false;
}
/// <summary>
/// Tries to get the hosted page ContentReference.
/// </summary>
/// <param name="data"><see cref="Submission.Data"/> dictionaty to be extended.</param>
/// <param name="contentLink">ContentReference to the hosted page.</param>
/// <returns>True hosted page ContentReference was resolved otherwise false.</returns>
public static bool TryGetHostedPage(this IDictionary<string, object> data, out ContentReference contentLink)
{
contentLink = null;
// hosted page type is string
if (data != null && data.TryGetValue(Constants.SYSTEMCOLUMN_HostedPage, out object hp) && hp is string hostedPage
&& ContentReference.TryParse(hostedPage, out contentLink))
{
return true;
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment