DOWNLOAD DEMO
Current Version: 2.4.0.4 2014-07-18
Need a hand? Join the community.

Implementing a Custom Form Submission Handler

Form Wizard Pro is an add on product for mojoPortal content management system available in our Store.  This article is primarily targeting developers who would like to extend the functionality of Form Wizard Pro with custom code to do additional processing of the submitted data after the form is submitted. Some experience with C# and Visual Studio is expected.

These are the basic steps involved in implementing a custom handler and plugging it in.

1. Create a Class Library project in Visual Studio, name it whatever you like, in this example I named it MyStuff, so MyStuff will be the default namespace and the class library will compile into an assembly named MyStuff.dll

2. Add a reference to the following:

  • sts.FormWizard.Business.dll
  • sts.FormWizard.Web.UI.dll
  • log4net.dll
  • mojoPortal.Business
  • System.Configuration

3. Create a class, the following example shows how to access the questions, answers, and any submitted files from code. The example just builds a string and then logs it to the mojoPortal log, but you could implement whatever logic you need to do whatever you need to do with the form data. In comments is  example code for sending an email with the submitted files attached.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using log4net;
using sts.Business;
using mojoPortal.Business;

namespace MyStuff
{
    public class MyCustomFormSubmissionHandlerProvider : FormSubmissionHandlerProvider
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(MyCustomFormSubmissionHandlerProvider));

        public MyCustomFormSubmissionHandlerProvider()
        { }

        public override void FormSubmittedEventHandler(object sender, FormSubmissionEventArgs e)
        {
            if (e == null) return;
            if (e.ResponseSet == null) return;
          
   log.Info("MyCustomFormSubmissionHandlerProvider called");

   StringBuilder results = new StringBuilder();

   results.Append(e.DetailUrl);
   results.Append("\r\n");
   results.Append("\r\n");

   //how to get the site user if the user was authenticated
   if (e.User != null)
   {
    results.Append("submitted by user " + e.User.Name);
    results.Append("\r\n");
   }

   //how to get the questions and answers

   List<WebFormQuestion> questionList = WebFormQuestion.GetByForm(e.ResponseSet.FormGuid);
   List<WebFormResponse> responses = WebFormResponse.GetByResponseSet(e.ResponseSet.Guid);

   foreach (WebFormQuestion question in questionList)
   {
    if (question.QuestionTypeId == 8) { continue; } //skip instruction block
    string response = GetResponse(e.ResponseSet.Guid, question.Guid, responses);
  
    results.Append("\r\n" + question.QuestionText + "\r\n");
    results.Append(response);
    results.Append("\r\n");
  
   }

   // how to get the information about submitted files

   if (e.Config.NotificationIncludeFiles && e.ResponseSet.UploadCount > 0)
   {
    results.Append("\r\n");

    List<FileAttachment> attachments = FileAttachment.GetListByItem(e.ResponseSet.Guid);
    
    string[] attachmentPaths = new string[attachments.Count];
    string[] attachmentNames = new string[attachments.Count];
    int i = 0;
    foreach (FileAttachment a in attachments)
    {
     string downloadPath = e.AttachmentBaseBath + a.ServerFileName;
     if (File.Exists(downloadPath))
     {
      attachmentPaths[i] = downloadPath;
      attachmentNames[i] = a.FileName;

      results.Append("submitted file " + a.FileName + " stored on disk as " + downloadPath);
      results.Append("\r\n");
     }

     i += 1;
    }

   }
   // how to send an email with the results and file attachments
                // you could get settings from config settings if you don't want to hard code it
                // you would need references to mojoPortal.Web.dll, mojoPortal.Business.dll, and mojoPortal.Net.dll
                // to use this commented code
                //string fromAddress = "noreply@yoursite.com";
                //string emailTo = "youradddress@yoursite.com";
                //string subject = "test";

                //Email.Send(
                //        SiteUtils.GetSmtpSettings(),
                //        fromAddress,
                //        string.Empty,
                //        emailTo,
                //        string.Empty,
                //        string.Empty,
                //        subject,
                //        results.ToString(),
                //        false,
                //        Email.PriorityNormal,
                //        attachmentPaths,
                //        attachmentNames);
   
             log.Info(results.ToString());
          
        }

        private string GetResponse(Guid responseSetGuid, Guid questionGuid, List<WebFormResponse> responses)
        {
            foreach (WebFormResponse response in responses)
            {
                if (
                    (response.ResponseSetGuid == responseSetGuid)
                    && (response.QuestionGuid == questionGuid)
                    )
                {
                    return response.Response;
                }
            }

            return string.Empty;
        }
    }
}

4. Compile your class library into a dll and put it in the /bin folder of your mojoPortal site.

5. Next we create a configuration file that plugs the custom handler in so it will be available for Form Wizard Pro. Create a text file named MyStuff.config as follows, and put it in the /FormWizard/SubmissionHandlers folder

<?xml version="1.0" encoding="utf-8" ?>
<FormSubmissionHandlers>
<providers>
  <add name="MyFormSubmissionHandler"
    type="MyStuff.MyCustomFormSubmissionHandlerProvider, MyStuff"
    description="This is my custom form submission handler " />
</providers>
</FormSubmissionHandlers>

6. Finally in the settings for an instance of Form Wizard Pro, you will see your handler in the dropdown list for "Submission Event Handler". Set your handler as the handler for the form and it will be invoked whenever the form is completed.

Last Updated 2011-05-11 by Joe Audette