Dynamicweb 8 Documentation
FormSaveProvider Class
Members  Example 

Inherit this type and override the Save method to create your own form save handler.
Syntax
'Declaration
 
Public Class FormSaveProvider 
   Inherits Dynamicweb.Extensibility.ConfigurableAddIn
public class FormSaveProvider : Dynamicweb.Extensibility.ConfigurableAddIn 
Example
Creating a form save provider
Imports Dynamicweb
Imports Dynamicweb.Extensibility
Imports Dynamicweb.Extensibility.Editors
Imports Dynamicweb.Modules.DataManagement

Imports System.Collections.Generic
Imports Dynamicweb.Rendering

Namespace Modules.DataManagement

    < _
    AddInName("DM.Form.SendMail"), _
    AddInLabel("Send the form over mail"), _
    AddInDescription("This will send an email with all form elements.") _
    > _
    Public Class MailFormSaveProvider
        Inherits FormSaveProvider
        Implements IDropDownOptions

        Private _MailTo As String = ""
        Private _MailFrom As String = ""
        Private _MailFromName As String = ""
        Private _MailFromUseField As Boolean = False
        Private _MailFromNameUseField As Boolean = False
        Private _MailCC As String = ""
        Private _MailBCC As String = ""
        Private _MailSubject As String = ""
        Private _MailTemplate As String = ""
        Private _MailEncoding As Text.Encoding = Text.Encoding.UTF8
        Private _MailLogging As Boolean = True

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.SenderName"), _
        AddInLabel("Sender name"), _
        AddInParameterEditor(GetType(Editors.TextParameterEditor), "NewGUI=true") _
        > _
        Public Property MailFromName() As String
            Get
                Return _MailFromName
            End Get
            Set(ByVal value As String)
                _MailFromName = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.UseFormFieldForSenderName"), _
        AddInLabel("Get from form"), _
        AddInParameterEditor(GetType(Editors.YesNoParameterEditor), "jsOnClick=ShowFieldSelector('MailFormSaveProvider.SenderName', this)") _
        > _
        Public Property MailFromNameUseField() As Boolean
            Get
                Return _MailFromNameUseField
            End Get
            Set(ByVal value As Boolean)
                _MailFromNameUseField = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.SenderEmail"), _
        AddInLabel("Sender e-mail"), _
        AddInParameterEditor(GetType(Editors.TextParameterEditor), "NewGUI=true") _
        > _
        Public Property MailFrom() As String
            Get
                Return _MailFrom
            End Get
            Set(ByVal value As String)
                _MailFrom = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.UseFormFieldForSenderEmail"), _
        AddInLabel("Get from form"), _
        AddInParameterEditor(GetType(Editors.YesNoParameterEditor), "jsOnClick=ShowFieldSelector('MailFormSaveProvider.SenderEmail', this)") _
        > _
        Public Property MailFromUseField() As Boolean
            Get
                Return _MailFromUseField
            End Get
            Set(ByVal value As Boolean)
                _MailFromUseField = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.RecipientEmail"), _
        AddInLabel("Recipient e-mail"), _
        AddInParameterEditor(GetType(Editors.TextParameterEditor), "NewGUI=true") _
        > _
        Public Property MailTo() As String
            Get
                Return _MailTo
            End Get
            Set(ByVal value As String)
                _MailTo = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.RecipientCCEmail"), _
        AddInLabel("Recipient CC e-mail"), _
        AddInParameterEditor(GetType(Editors.TextParameterEditor), "NewGUI=true") _
        > _
        Public Property MailCC() As String
            Get
                Return _MailCC
            End Get
            Set(ByVal value As String)
                _MailCC = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.RecipientBCCEmail"), _
        AddInLabel("Recipient BCC e-mail"), _
        AddInParameterEditor(GetType(Editors.TextParameterEditor), "NewGUI=true") _
        > _
        Public Property MailBCC() As String
            Get
                Return _MailBCC
            End Get
            Set(ByVal value As String)
                _MailBCC = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.Subject"), _
        AddInLabel("Subject"), _
        AddInParameterEditor(GetType(Editors.TextParameterEditor), "NewGUI=true") _
        > _
        Public Property MailSubject() As String
            Get
                Return _MailSubject
            End Get
            Set(ByVal value As String)
                _MailSubject = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.Template"), _
        AddInLabel("Template"), _
        AddInParameterEditor(GetType(Editors.TemplateParameterEditor), "folder=/Templates/DataManagement/Forms/Email;FullPath=false;NewGUI=true") _
        > _
        Public Property MailTemplate() As String
            Get
                Return _MailTemplate
            End Get
            Set(ByVal value As String)
                _MailTemplate = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.Encoding"), _
        AddInLabel("Encoding"), _
        AddInParameterEditor(GetType(Editors.DropDownParameterEditor), "NewGUI=true;SortBy=Value") _
        > _
        Public Property MailEncodingCodePage() As Integer
            Get
                Return _MailEncoding.CodePage
            End Get
            Set(ByVal value As Integer)
                Dim enc As Text.Encoding = Text.Encoding.GetEncoding(value)
                _MailEncoding = enc
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.UseLogging"), _
        AddInLabel("Logging"), _
        AddInParameterEditor(GetType(Editors.YesNoParameterEditor), "") _
        > _
        Public Property MailLogging() As Boolean
            Get
                Return _MailLogging
            End Get
            Set(ByVal value As Boolean)
                _MailLogging = value
            End Set
        End Property

        Public Function GetOptions(ByVal name As String) As Hashtable Implements IDropDownOptions.GetOptions
            Dim ht As New Hashtable()
            Dim encArray() As Text.EncodingInfo = Text.Encoding.GetEncodings
            'Array.Sort(encArray, AddressOf CompareDisplayName)

            For Each enc As Text.EncodingInfo In encArray
                If Not ht.ContainsKey(enc.CodePage) Then
                    ht.Add(enc.CodePage, enc.DisplayName)
                End If
            Next

            Return ht
        End Function

        Public Overrides Function Save(ByVal Form As FormSetting, ByVal KeysAndValues As Dictionary(Of String, Object)) As Boolean
            Dim sb As New Text.StringBuilder
            Dim isSaved As Boolean = False

            Dim hasError As Boolean = False

            Dim SendMail As Boolean = False
            Dim DWSmtpServer As String = ""

            Dim MailFromEmail As String = ""
            Dim MailFromNameValue As String = ""

            Dim MailTemplateContext As String = ""

            Try
                '############################
                'Building template context
                '############################
                Dim template As New Template("DataManagement/Forms/Email/" & MailTemplate)

                Dim fields As FormFieldCollection = Form.Fields

                Dim fieldsTmp As New FormFieldCollection
                fieldsTmp.AddRange(fields)

                For Each field As FormField In fieldsTmp
                    If KeysAndValues.ContainsKey(field.SystemName) Then
                        field.Value = Base.ChkString(KeysAndValues(field.SystemName))
                    Else
                        Form.Fields.Remove(field)
                    End If
                Next
                Dim render As New FormRenderer()

                MailTemplateContext = render.RenderForm(Form, template)

                '############################
                'Email settings
                '############################
                DWSmtpServer = Base.ChkString(Base.GetGs("/Globalsettings/System/MailServer/Server"))

                'Set values for MailFromNameValue and MailFromEmail. They may be overridden later.
                MailFromNameValue = MailFromName
                MailFromEmail = IIf(String.IsNullOrEmpty(MailFrom), Base.ChkString(Base.GetGs("/Globalsettings/Settings/CommonInformation/Email")), MailFrom).ToString
                If MailFromUseField OrElse MailFromNameUseField Then
                    For Each key As String In KeysAndValues.Keys
                        If key = MailFrom Then
                            MailFromEmail = Base.ChkString(KeysAndValues(key))
                        End If
                        If MailFromNameUseField AndAlso key = MailFromName Then
                            MailFromNameValue = Base.ChkString(KeysAndValues(key))
                        End If
                    Next
                End If
                If String.IsNullOrEmpty(MailFrom) Then MailFrom = "noreply@dynamicweb.dk"


                Dim objEmail As New System.Net.Mail.MailMessage

                If _MailEncoding IsNot Nothing Then
                    objEmail.BodyEncoding = _MailEncoding
                Else
                    objEmail.BodyEncoding = Text.Encoding.UTF8
                End If

                If Not Base.ValidateEmail(MailTo) Then
                    SetInvalidEmailMessage(MailTo)
                End If
                objEmail.To.Add(MailTo)

                If Not String.IsNullOrEmpty(MailCC) Then
                    If MailCC.Contains(";") Then
                        Dim MailCCArray() As String = MailCC.Split(";"c)

                        For Each address As String In MailCCArray
                            Dim addr As String = address.Trim()
                            If Not Base.ValidateEmail(addr) Then
                                SetInvalidEmailMessage(addr)
                            End If
                            objEmail.CC.Add(addr)
                        Next
                    Else
                        If Not Base.ValidateEmail(MailCC) Then
                            SetInvalidEmailMessage(MailCC)
                        End If
                        objEmail.CC.Add(MailCC)
                    End If
                End If

                If Not String.IsNullOrEmpty(MailBCC) Then
                    If MailBCC.Contains(";") Then
                        Dim MailBCCArray() As String = MailBCC.Split(";"c)

                        For Each address As String In MailBCCArray
                            Dim addr As String = address.Trim()
                            If Not Base.ValidateEmail(addr) Then
                                SetInvalidEmailMessage(addr)
                            End If
                            objEmail.Bcc.Add(addr)
                        Next
                    Else
                        If Not Base.ValidateEmail(MailBCC) Then
                            SetInvalidEmailMessage(MailBCC)
                        End If
                        objEmail.Bcc.Add(MailBCC)
                    End If
                End If

                If String.IsNullOrEmpty(MailFromEmail) OrElse Not Base.ValidateEmail(MailFromEmail) Then
                    MailFromEmail = MailTo

                    If String.IsNullOrEmpty(MailFromEmail) OrElse Not Base.ValidateEmail(MailFromEmail) Then
                        MailFromEmail = "noreply@dynamicweb-cms.com"
                    End If
                End If

                If String.IsNullOrEmpty(MailFromNameValue) Then
                    MailFromNameValue = MailFromEmail
                End If

                If Not String.IsNullOrEmpty(MailFromNameValue) Then
                    objEmail.Sender = New System.Net.Mail.MailAddress(MailFromEmail, MailFromNameValue)
                Else
                    objEmail.Sender = New System.Net.Mail.MailAddress(MailFromEmail)
                End If
                objEmail.From = objEmail.Sender
                objEmail.Subject = MailSubject

                objEmail.IsBodyHtml = True
                objEmail.Body = MailTemplateContext

                isSaved = EmailHandler.Send(objEmail, MailLogging)
                objEmail.Dispose()

            Catch ex As DataException
                isSaved = False
            End Try

            Return isSaved
        End Function

        Private Sub SetInvalidEmailMessage(ByVal mailAddress As String)
            HttpContext.Current.Response.Write(String.Format("<script>alert('Email address: \'{0}\' is invalid.');history.back(-1);</script>", mailAddress))
            HttpContext.Current.Response.End()
        End Sub

        Private Function CompareDisplayName(ByVal first As Text.EncodingInfo, ByVal second As Text.EncodingInfo) As Integer
            Return String.Compare(first.DisplayName, second.DisplayName, True)
        End Function
    End Class

End Namespace

Inheritance Hierarchy

System.Object
   Dynamicweb.Extensibility.ConfigurableAddIn
      Dynamicweb.Modules.DataManagement.FormSaveProvider
         Dynamicweb.Modules.DataManagement.DefaultFormSaveProvider
         Dynamicweb.Modules.DataManagement.MailFormSaveProvider
         Dynamicweb.Modules.DataManagement.MailReceiptSaveProvider
         Dynamicweb.Modules.DataManagement.SaveInCSVFile

Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

FormSaveProvider Members
Dynamicweb.Modules.DataManagement Namespace

Send Feedback