Form Data Export Job Issue


Hi all, I have a job scheduled to run nightly, which exports all form data as XML and places it in a folder for that day, see code below.

This is working perfectly for all exisiting forms. However, I have created a number of new pages and forms - which are not being exported by this job (the old pages/ forms are still being exported just fine).

What could be causing the new forms to not export? I am also noticing the following 2 errors in the logs: "Object reference not set to an instance of an object" and "The given path's format is not supported"

Any help would be appreciated!

Here is the code for the export job:

using System;
using System.Text;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Xml;
using System.Net.Mail;
using System.IO;

using EPiServer;
using EPiServer.DataAccess;
using EPiServer.DataAbstraction;
using EPiServer.Core;
using EPiServer.BaseLibrary.Scheduling;
using EPiServer.PlugIn;
using EPiServer.Filters;
using EPiServer.Security;
using EPiServer.XForms;
using Brookson.Templates.PageTypes;
using PageTypeBuilder;
using EPiServer.Web;

namespace Brookson.Scheduler.FormsExport
    [ScheduledPlugIn(DisplayName = "Form Data Exporter",
        Description = "Extracts Form Data from all forms in the website.",
        SortIndex = 110)]
    public class FormDataExport : JobBase
        private bool _stop;
        private DateTime _fromDate, _toDate;
        private string OutDir;

        public FormDataExport()
            : base()
            IsStoppable = true;

        public override void Stop()
            _stop = true;

        public override string Execute()
            if (_stop)
                return "Job has been stopped";

            StringBuilder formInfo = new StringBuilder(2000);


                formInfo.AppendFormat("Date range {0:dd/MM/yyyy HH:mm:ss} to {1:dd/MM/yyyy HH:mm:ss}",
                    _fromDate, _toDate);


                foreach (XForm xform in XForm.GetForms())
                    Dictionary<string, StringBuilder> formData = new Dictionary<string, StringBuilder>();
                    Dictionary<string, int> formFreq = new Dictionary<string, int>();

                    int numData = 0;

                    foreach (XFormData xdata in xform.GetPostedData(_fromDate, _toDate))
                        string pageNameKey = GetPageName(xdata.PageGuid);
                        if (string.IsNullOrWhiteSpace(pageNameKey))

                        if (!formData.ContainsKey(pageNameKey))
                            formData.Add(pageNameKey, new StringBuilder(10000));
                            formFreq.Add(pageNameKey, 0);



                    foreach (var f in formData)
                        SaveXmlAsFile(f.Key, f.Value.ToString());

                    formInfo.AppendFormat("Form: {0}, {1} (", xform.FormName, numData);

                    foreach (var f in formFreq)
                        formInfo.AppendFormat("{0} = {1};", f.Key, f.Value);

            catch (Exception ex)
                return "Failed! - " + ex.Message + ((ex.InnerException != null) ? ex.InnerException.Message : string.Empty);


            return formInfo.ToString().Replace("\r\n", "<br />");

        /// <summary>
        /// Sets up the environment required by this scheduled job.
        /// </summary>
        private void Setup()
            OutDir = @"C:\EPiServer\Sites\FormData\" + DateTime.Now.ToString("yyyyMMdd_MMM_dd_yyyy");

            if (!Directory.Exists(OutDir))

            DateTime yesterday = DateTime.Now.AddDays(-1.0);
            DateTime fourdaysago = DateTime.Now.AddDays(-4.0);

            _fromDate = new DateTime(fourdaysago.Year, fourdaysago.Month, fourdaysago.Day);
            _toDate = new DateTime(yesterday.Year, yesterday.Month, yesterday.Day, 23, 59, 59);

        private string GetPageName(Guid guid)
            PermanentPageLinkMap map = PermanentLinkMapStore.Find(guid) as PermanentPageLinkMap;

            if (map != null && !PageReference.IsNullOrEmpty(map.PageReference))
                PageData pd = DataFactory.Instance.GetPage(map.PageReference);

                if (pd != null)
                    return pd.PageName;

            return null;

        private string GetXmlData(XFormData xdata)
            StringBuilder xml = new StringBuilder(1000);

            xml.AppendLine(" <record>");

            xml.AppendFormat("  <{0}>{1}</{0}>", "FullDateTime", xdata.DatePosted.ToString("yyyy-MM-dd HH:mm:ss"));

            XmlNode instanceNode = xdata.Data.SelectSingleNode("/instance");

            foreach (XmlNode node in instanceNode.ChildNodes)
                xml.AppendFormat("  <{0}>{1}</{0}>", node.Name, HttpUtility.HtmlEncode(node.InnerText));

            xml.AppendLine(" </record>");

            return xml.ToString();

        private void SaveXmlAsFile(string filename, string contents)
            filename = OutDir + "\\" + filename.Replace(" ", "_") + ".xml";

            using (StreamWriter sw = File.CreateText(filename))

        private void SaveInfoAsFile(string info)
            string filename = OutDir + "\\info.txt";

            using (StreamWriter sw = File.CreateText(filename))



Dec 31, 2012 10:47

Why not just put a break point in there, run the application locally in debug mode and trigger the scheduled job manually, to see the error details, or log the stack trace somewhere.


Jan 01, 2013 19:19
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.