Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Web Forms for Marketers rev. 141217 manual fixes script
<%@ WebHandler Language="C#" Class="PostWFFM800Script" %>
using System;
using System.Linq;
using Sitecore.sitecore.admin;
using Sitecore.Data.Items;
using Sitecore.Data;
using System.Xml.Linq;
using System.Collections.Generic;
using Sitecore.SecurityModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Linq;
/// <summary>
/// Post WFFM 800 script to do some manual fixing of items after .
/// </summary>
public class PostWFFM800Script : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Fixing or removing event elements from __Tracking field of Form items..." + Environment.NewLine);
// As per upgrade guide at https://dev.sitecore.net/~/media/3161DF27F28F4506A5EF18BC0CF7B80A.ashx (must be signed in to download)
// If errors occur you need to manually fix some items, but we have a lot so let's script it
var masterDb = Sitecore.Data.Database.GetDatabase("master");
var formItemTid = new ID("{FFB1DA32-2764-47DB-83B0-95B843546A7E}");
var formModuleFolder = masterDb.GetItem(new ID("{D81AC026-07B6-4929-9588-40ACBDC56435}"));
var formItems = formModuleFolder
.Axes.GetDescendants()
.Where(i => i.TemplateID == formItemTid);
var goalsAndPageEvents = new List<Item>();
goalsAndPageEvents.AddRange(masterDb.GetItem(new ID("{0CB97A9F-CAFB-42A0-8BE1-89AB9AE32BD9}")).Axes.GetDescendants().Where(i => i.TemplateName == "Goal").ToList());
goalsAndPageEvents.AddRange(masterDb.GetItem(new ID("{633273C1-02A5-4EBC-9B82-BD1A7C684FEA}")).Axes.GetDescendants().Where(i => i.TemplateName == "Page Event").ToList());
foreach (Item formItem in formItems)
{
var changed = false;
var trackingFieldValue = formItem["__Tracking"];
var trackingFieldXml = XDocument.Parse(trackingFieldValue);
// For every <event> element, ensure it has an id, else try look one up, else delete.
foreach (var eventElement in trackingFieldXml.Root.Elements())
{
var attributes = eventElement.Attributes();
if (!attributes.Any(a => a.Name == "id") && attributes.Any(a => a.Name == "name"))
{
changed = true;
var name = attributes.First(a => a.Name == "name").Value;
var goalOrPageEvent = goalsAndPageEvents.FirstOrDefault(i => i.Name == name);
if (goalOrPageEvent == null)
{
eventElement.Remove();
}
else
{
eventElement.SetAttributeValue("id", goalOrPageEvent.ID.ToString());
}
}
}
// Update if necessary
var trackingFieldValueNew = trackingFieldXml.ToString();
if (changed && trackingFieldValueNew != trackingFieldValue)
{
context.Response.Write(string.Format("Changing '{0}' from __Tracking of: '{1}'...", formItem.Name, trackingFieldValue));
using (new SecurityDisabler())
{
//formItem.Editing.BeginEdit();
//formItem["__Tracking"] = trackingFieldValueNew;
//formItem.Editing.EndEdit();
}
context.Response.Write(string.Format("{0}To '{1}'{0}{0}", Environment.NewLine, trackingFieldValueNew));
}
}
context.Response.Write(Environment.NewLine + Environment.NewLine + "Done!");
}
public bool IsReusable
{
get
{
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment