Skip to content

Instantly share code, notes, and snippets.

@Krusen Krusen/FixWebforms.aspx
Last active Feb 4, 2017

Embed
What would you like to do?
Sitecore WFFM: Script for removing old "Save to database" save action and removing <host> and <from> parameters from forms after upgrading
<%@ Page Language="C#" %>
<%@ Import Namespace="Sitecore.Diagnostics" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
var formTemplateId = Sitecore.Data.ID.Parse("{FFB1DA32-2764-47DB-83B0-95B843546A7E}");
// The root item containing your web forms to be fixed
var webformsFolder = Sitecore.Configuration.Factory.GetDatabase("master").GetItem("{F1F7AAB6-C8CE-422F-A214-F610C109FA63}");
var webforms = webformsFolder.Axes.GetDescendants().Where(x => x.TemplateID == formTemplateId).ToList();
var saveToDbActionsRemoved = 0;
var parametersUpdated = 0;
var errors = 0;
var hostRegex = @"&lt;host&gt;.*?&lt;\/host&gt;";
var fromRegex = @"&lt;from&gt;.*?&lt;\/from&gt;";
foreach (var webform in webforms)
{
try
{
var saveActionXml = XElement.Parse(webform["Save Actions"]);
var saveToDatabaseAction = saveActionXml.Element("g").Elements("li").FirstOrDefault(x => x.Attribute("id").Value == "{F03608CC-559B-451E-BC5B-6A7ADFF32C94}");
if (saveToDatabaseAction != null)
{
Log.Info("[FixWebforms] "+webform.Paths.ContentPath+" "+webform.ID+" | Removing 'SaveToDatabase' action", this);
saveToDatabaseAction.Remove();
saveToDbActionsRemoved++;
}
var parametersElements = saveActionXml.Element("g")
.Elements("li")
.Where(x => x.Attribute("id").Value == "{D4502A11-9417-4479-9F2A-485F45D2E2D0}")
.Select(x => x.Element("parameters"))
.ToList();
var didUpdateParameters = false;
foreach (var parameters in parametersElements)
{
if (Regex.IsMatch(parameters.Value, hostRegex))
{
Log.Info("[FixWebforms] "+webform.Paths.ContentPath+" "+webform.ID+" | Removing 'host' parameter", this);
parameters.Value = Regex.Replace(parameters.Value, hostRegex, "");
didUpdateParameters = true;
}
if (Regex.IsMatch(parameters.Value, fromRegex))
{
Log.Info("[FixWebforms] "+webform.Paths.ContentPath+" "+webform.ID+" | Removing 'from' parameter", this);
parameters.Value = Regex.Replace(parameters.Value, fromRegex, "");
didUpdateParameters = true;
}
}
if (didUpdateParameters)
parametersUpdated++;
webform.Editing.BeginEdit();
{
webform["Save Actions"] = saveActionXml.ToString(SaveOptions.DisableFormatting);
}
webform.Editing.EndEdit();
}
catch (Exception ex)
{
Log.Error("[FixWebforms] Failed to fix "+webform.Paths.ContentPath+" "+webform.ID, ex, this);
errors++;
}
}
Log.Info("[FixWebforms] Webforms: "+ webforms.Count, this);
Log.Info("[FixWebforms] Removed 'SaveToDatabase' actions: " + saveToDbActionsRemoved, this);
Log.Info("[FixWebforms] Removed parameters: " + parametersUpdated, this);
Log.Info("[FixWebforms] Errors: " + errors, this);
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Title</title>
</head>
<body>
<form id="HtmlForm" runat="server">
<div>
</div>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.