Skip to content

Instantly share code, notes, and snippets.

@lowedown
Created October 1, 2018 16:16
Show Gist options
  • Save lowedown/016cd76f88acd0388da8d37e2a7f6267 to your computer and use it in GitHub Desktop.
Save lowedown/016cd76f88acd0388da8d37e2a7f6267 to your computer and use it in GitHub Desktop.
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="Sitecore.Analytics.Data.Items" %>
<%@ Import Namespace="Sitecore.Data" %>
<%@ Import Namespace="Sitecore.Data.Items" %>
<%@ Import Namespace="Sitecore.Diagnostics" %>
<%@ Import Namespace="Sitecore.Extensions.XElementExtensions" %>
<%@ Import Namespace="Sitecore.Form.Core.Configuration" %>
<%@ Import Namespace="Sitecore.Marketing.Definitions.Goals" %>
<%@ Import Namespace="Sitecore.WFFM.Abstractions.Analytics" %>
<%@ Import Namespace="Sitecore.WFFM.Abstractions.Dependencies" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Xml.Linq" %>
<%@ Import Namespace="System.Xml.XPath" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Run WFFM Upgrade 8.2</h1>
<asp:Button runat="server" ID="runButton" OnClick="runButton_OnClick" Text="Run Upgrade Steps" />
<asp:Literal runat="server" id="message"/>
</div>
<script language="C#" runat="server">
protected void runButton_OnClick(object sender, EventArgs e)
{
UpdateFormsTrackingField();
}
private void UpdateFormsTrackingField()
{
var locationOfForms = "/sitecore/content/Global Shared Data/Forms/Website/"; // Change this in case you are using a custom location
Sitecore.Data.Database masterDatabase = StaticSettings.MasterDatabase;
Sitecore.Data.Items.Item[] array = masterDatabase.SelectItems(string.Format(locationOfForms + "/*[@@templateid='{0}']", Sitecore.WFFM.Abstractions.Analytics.IDs.FormTemplateID));
Sitecore.Data.Items.Item[] array2 = array;
var itemsUpdated = 0;
for (int i = 0; i < array2.Length; i++)
{
Sitecore.Data.Items.Item item = array2[i];
try
{
string value = item.Fields["__Tracking"].Value;
message.Text += item.Paths.FullPath + "<br/>";
XDocument xDocument = XDocument.Parse(value);
IEnumerable<XElement> enumerable = xDocument.XPathSelectElements("//tracking/event");
foreach (XElement current in enumerable)
{
string attributeValue = current.GetAttributeValue("id");
if (string.IsNullOrEmpty(attributeValue))
{
string attributeValue2 = current.GetAttributeValue("name");
if (!string.IsNullOrEmpty(attributeValue2))
{
Sitecore.Data.ID iD = Sitecore.Data.ID.Null;
IGoalDefinition goal = DependenciesManager.AnalyticsTracker.GetGoal(new Sitecore.Data.ID(attributeValue));
if (goal != null)
{
iD = new ID(goal.Id);
}
else
{
PageEventItem pageEventItem = DependenciesManager.AnalyticsTracker.DefinitionItems.PageEvents[attributeValue2];
if (pageEventItem != null)
{
iD = pageEventItem.ID;
}
}
if (iD != Sitecore.Data.ID.Null)
{
current.Remove();
}
else
{
current.Add(new XAttribute("id", iD.Guid));
}
itemsUpdated++;
message.Text += "<br/>Updated: " + item.Paths.FullPath;
}
}
}
item.Editing.BeginEdit();
item.Fields["__Tracking"].Value = xDocument.ToString();
item.Editing.EndEdit();
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error(ex.Message, ex);
message.Text = ex.Message;
}
}
message.Text += "<br/>Done. Updated:" + itemsUpdated;
}
</script>
</form>
</body>
</html>
@lowedown
Copy link
Author

lowedown commented Oct 1, 2018

Update script for Sitecore 9 when upgrading WFFM from before 8.2. The built-in code will fail during execution because of changed API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment