Provider.cs

Version InfoThis topic applies to Episerver.ConnectForMarketingAutomation 4.0.0 and lower. For later versions, see Sample Connector - IMarketingConnector

using System;
using System.IO;
using System.Text;
using EPiServer.MarketingAutomationIntegration.Core;
using EPiServer.ServiceLocation;
using EPiServer.XForms.Util;
using EPiServer.MarketingAutomationIntegration.Services;
using EPiServer.Framework.Localization;
using System.Web;
using System.Xml;
using EPiServer.MarketingAutomationIntegration.Helpers;

namespace DemoConnector.Services
{
    /// <summary>
    /// This is the main interface that the marketing automation Interface (MAI)
    /// plugin uses to load the connector. During the intitialize process, MAI uses
    /// the Service locator to load the connector. 
    /// Note: Only one connector can exist in the solution or be installed with the MAI framework.
    /// </summary>
    [ServiceConfiguration(ServiceType = typeof(IProvider), Lifecycle = ServiceInstanceScope.Singleton)]
    public class Provider : ProviderBase
    {
        /// <summary>
        /// The name of the connector.
        /// </summary>
        public override string Name
        {
            get { return "DemoConnector"; }
        }

        /// <summary>
        /// The settings for the connector.
        /// </summary>
        /// <remarks>
        /// Typically, use an IDynamicData implementation to store configuration data required for the connector. 
        /// This connector has no configuration data.
        /// </remarks>
        public override IProviderSettings Settings
        {
            get { return this.Settings; }
        }

        /// <summary>
        /// As a connector author, you can attach a url to the main connector button
        /// at the top of the configuration screen. You can attach an external url, 
        /// the url of the configuration page, or another url of a page of your choosing.
        /// </summary>
        /// <returns>A url.</returns>
        /// <remarks>
        /// This code demonstates how to re-direct the url to the location
        /// of the configuration page for the connector.
        /// </remarks>
        public override string GetDashboardUrl()
        {
            //Build EPiServer URL to the configuration page
            var uri = new Uri(HttpContext.Current.Request.Url.ToString());
            var port = string.Empty;
            if (uri.Port != 80)
                port = ":" + uri.Port;

            var requested = uri.Scheme + "://" + uri.Host + port;
            string settingsUrl = ModuleHelper.GetConnectorSettingsUrlString();
            return string.Format("{0}{1}", requested, settingsUrl);

            // Or return the url to the end point if there is one.
            // return "http://www.google.com";
        }

        /// <summary>
        /// Called by the MAI framework at startup, you can use this method  
        /// to add services to the default Episerver IoC container.
        /// </summary>
        /// <param name="serviceConfigurationContext">The service context </param>
        public override void Init(ServiceConfigurationContext serviceConfigurationContext)
        {
            serviceConfigurationContext.Container.Configure(sp =>
            {
                //List or Contact List Service.
                sp.For<IListService>().Use<ListService>();

                //Targeting content services.
                sp.For<IProfileService>().Use<ProfileService>();
                sp.For<IProgramService>().Use<ProgramService>();
                sp.For<IScoringService>().Use<ScoringService>();

                //Interface for authentication with the provider
                sp.For<IAuthenticationService>().Use<AuthenticationService>();
                sp.For<IProviderServices>().Use<ProviderServices>();
            });
        }

        /// <summary>
        /// Called periodically by the MAI framework to verify that the connector is ready to use.
        /// </summary>
        /// <returns>Result stating whether the framework is ready for use.</returns>
        public override bool IsReady()
        {
            return true;
        }

        /// <summary>
        /// Temporary static cache to hold form data from the process function.
        /// </summary>
        public static class FormData
        {
            /// <summary>
            /// Sample string to hold processed data from our demo connector form.
            /// </summary>
            public static string Output { get; set; }
        }

        /// <summary>
        /// This function received the connector data that was collected from the user.  Read it in as Xml and 
        /// process it however you like.  This loops over the Xml tags and parses out data for each one for processing.
        /// 
        /// This code is executed when the Submit button is clicked on a form.
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">Form data is passed in through this. You can retrieve the data as innerHtml or Xml.
        /// You also can loop through the nodes to get the user data.</param>
        public override void ProcessXFormData(object sender, XFormDataEventArgs e)
        {
            //  This is where one has access to the form data from the user.  This is where the business logic would start.

            var output = new StringBuilder();
            var innerXml = e.FormData.Data.InnerXml;

            // Form data comes out as xml, it can easily be read and then do whatever you want with it.
            using (var reader = XmlReader.Create(new StringReader(innerXml)))
            {
                var ws = new XmlWriterSettings {Indent = true};

                using (var writer = XmlWriter.Create(output, ws))
                {
                    // Read each line  of xml and act accordingly. In this case we are just writing it back out as xml
                    // as an example.
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                            // this is the start element tag
                            case XmlNodeType.Element:
                                writer.WriteStartElement(reader.Name);
                                break;
                            // this is the data associated with the current tag
                            case XmlNodeType.Text:
                                // process input data
                                writer.WriteString(reader.Value);
                                break;
                            // this is the end tag for the current data element
                            case XmlNodeType.EndElement:
                                writer.WriteFullEndElement();
                                break;
                        }
                    }
                }
            }

            // temp cache storage for our form data
            FormData.Output = output.ToString();
        }
    }
}

Related topics

Last updated: Dec 14, 2015