Sitecore: Export to XML of Web Form For Marketers (WFFM) Form Reports


After Export CSV/Excel and Display Web Forms for Marketers Form Reports in HTML of form front end, let’s Export XML file of WFFM data.

To Export XML form front end created a class, this class is responsible to generate XML file of form data:


public class CustomExportToXML
{
    public string Process(FormItem form, FormPacket Packet)
    {
    Job job = Context.Job;
    if (job != null)
    {
        job.Status.LogInfo(ResourceManager.Localize("EXPORTING_DATA"));
     }
     XmlDocument xmlDocument = new XmlDocument();
     xmlDocument.InnerXml = Packet.ToXml();
     string exportRestriction = FormRegistryUtil.GetExportRestriction(form.ID.ToString(),       string.Empty);
     ListString listString = new ListString(exportRestriction);
     XmlNodeList xmlNodeList = xmlDocument.SelectNodes("packet/formentry");
     foreach (string current in listString)
     {
        foreach (XmlNode xmlNode in xmlNodeList)
        {
            XmlAttribute xmlAttribute = xmlNode.Attributes[current];
           if (xmlAttribute != null)
           {
              xmlNode.Attributes.Remove(xmlAttribute);
           }
              XmlNodeList xmlNodeList2 = xmlNode.SelectNodes(string.Format("field[@fieldid='{0}']", current));
              foreach (XmlNode oldChild in xmlNodeList2)
              {
                 xmlNode.RemoveChild(oldChild);
              }
           }
       }
       return xmlDocument.DocumentElement.OuterXml;
   }
}

Another class to write code of Export XML of selected WFFM form:


public static void ExportToXMLFile(string formID, string filename){
    ItemUri uri = new ItemUri(Sitecore.Data.ID.Parse(formID), Sitecore.Context.Database);

    //1. Get Form
    FormItem form = new FormItem(Database.GetItem(uri));
    string name = form.FormName;

    //2. Data Filters
    List<GridFilter> filters = new List<GridFilter>();
    // 2.a Form filter
    //filters.Add(new GridFilter(Sitecore.Form.Core.Configuration.Constants.DataKey, formID, GridFilter.FilterOperator.Contains));
    //// 2.b Get archived items
    //filters.Add(new GridFilter(Sitecore.Form.Core.Configuration.    Constants.StorageName, Sitecore.Form.Core.Configuration.Constants.Archive, GridFilter.FilterOperator.Contains));

    filters.Add(new GridFilter("storageName", string.Empty, GridFilter.FilterOperator.Contains));
    filters.Add(new GridFilter("dataKey", formID, GridFilter.FilterOperator.Contains));

   //3. Get all entries
   IEnumerable<IForm> entries = Sitecore.Forms.Data.DataManager.GetForms().GetPage(new PageCriteria(0, 0x7ffffffe), null, filters);

   // 3.a Apply custom filtering on the entries
   //entries = entries.Where(a => a.Timestamp.Date.CompareTo(startDate) >= 0 && a.Timestamp.Date.CompareTo(endDate) <= 0);

   //4. Create a form packet
   FormPacket packet = new FormPacket(entries);

   CustomExportToXML export = new CustomExportToXML();
   string result = export.Process(form, packet);
   HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".xml");
   HttpContext.Current.Response.ContentType = "text/xml";
   HttpContext.Current.Response.Write(result);
   HttpContext.Current.Response.End();
}


And at last I added a button to download XML file in web form (.aspx) page: 


protected void btnXmlDownload_Click(object sender, EventArgs e)
{
    try
    {
        if (ddlForms.SelectedValue.ToString() != "0")
        {
           pnlError.Visible = false;
           string formID = ddlForms.SelectedValue.ToString();
           string fileName = "custom";
           if (!string.IsNullOrEmpty(txtOutputFileName.Text))
           {
               fileName = txtOutputFileName.Text.Replace(".", "_");
           }
           ExportToExcelCommon.ExportToXMLFile(formID, fileName);
         }
    }
    catch (Exception ex)
    {
        Sitecore.Diagnostics.Log.Error("Error in Custom Export To XML", ex, this);
    }
}

 

Advertisements
This entry was posted in Sitecore and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s