jxycms
2/2/2017 - 11:14 PM

get data from customer table and create xml file and put it in root

get data from customer table and create xml file and put it in root

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

using CMS;
using CMS.DataEngine;
using CMS.EmailEngine;
using CMS.EventLog;
using CMS.Helpers;
using CMS.MacroEngine;
using CMS.Membership;
using CMS.Scheduler;
using CMS.SiteProvider;
using CMS.CustomTables;

[assembly: RegisterCustomClass("HSGA.GenerateXMLTask", typeof(HSGA.GenerateXMLTask))]
namespace HSGA
{
    public class GenerateXMLTask : ITask
    {
        protected const string SOURCE = "GenerateXMLTask";

        public string Execute(TaskInfo task)
        {

            try
            {
                EventLogProvider.LogEvent(EventType.INFORMATION, "GenerateXMLTask", "EXECUTE", "Task is starting.");

                GenerateXMLFile("customtable.StateInterestEmailAddress", "xml-officelookup.xml");
                GenerateXMLFile("customtable.HSGA_Office", "xml-offices.xml");

                EventLogProvider.LogEvent(EventType.INFORMATION, "GenerateXMLTask", "EXECUTE", "Task ends.");
                return String.Format("Execute successfully on {0}", DateTime.Now);
            }

            catch (Exception ex)
            {
                EventLogProvider.LogException(SOURCE, "EXECUTE", ex);
                return String.Format("Execute failed on {0}: please see EventLog for more details.", DateTime.Now);
            }

        }

        private bool GenerateXMLFile(string customTableClassName, string xmlFileName)
        {
            DataClassInfo customTable = DataClassInfoProvider.GetDataClassInfo(customTableClassName);
            if (customTable != null)
            {
                string xmlFile = String.Format("{0}{1}", System.Web.HttpContext.Current.Server.MapPath("~/"), xmlFileName);
                EventLogProvider.LogEvent(EventType.INFORMATION, "GenerateXMLTask", "XML FILE LOCATION", String.Format("Xml File location: {0}", xmlFile));

                DataSet dataSet = CustomTableItemProvider.GetItems(customTableClassName);
                dataSet.WriteXml(xmlFile, XmlWriteMode.WriteSchema);
            }

            return true;
        }

    }
}

1. create a file called xxx.cs
2. copy the code above and put the file in /App_Code/Custom
3. go to kentico--->schedule task
4. create a new task and choose this file