Hi,
I am developing a new site for a client that does not use any fixed datastore. This is not a problem as they do not want a CMS as the content will remain fairly static.
They wanted to use a data collection form for customers but also find a way of importing this into their own internal CRM.
I found the best way was to extend the contact form used in the TBH to generate an XML document on the fly and attach it to the MailMessage object used to send a message to the Contact form mail address specified in web.config.
They can then import this into other data stores if required.
Thought i would post the code here in case it's of any use to anyone:
Code:
Imports System.Net.Mail
Imports System.IO
Imports System.Xml
Namespace Retro.Web.UI
Partial Class Contact
Inherits BasePage
'write in memory
Dim msXml As MemoryStream = New MemoryStream()
Dim objWriter As XmlTextWriter
Protected Sub txtSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSubmit.Click
Try
' send the mail
Dim msg As New MailMessage
msg.IsBodyHtml = False
msg.From = New MailAddress(txtEmail.Text, txtName.Text)
msg.To.Add(New MailAddress(Globals.Settings.ContactForm.MailTo))
If Not String.IsNullOrEmpty(Globals.Settings.ContactForm.MailCC) Then
msg.CC.Add(New MailAddress(Globals.Settings.ContactForm.MailCC))
End If
msg.Subject = String.Format( _
Globals.Settings.ContactForm.MailSubject, txtSubject.Text)
msg.Body = txtBody.Text
'generate XML file and add as an attachment
GenerateXml(msg, txtName.Text, txtEmail.Text, txtSubject.Text, txtBody.Text)
Dim client As New SmtpClient()
client.Send(msg)
' show a confirmation message, and reset the fields
lblFeedbackOK.Visible = True
lblFeedbackKO.Visible = False
txtName.Text = ""
txtEmail.Text = ""
txtSubject.Text = ""
txtBody.Text = ""
Exit Try
Catch ex As Exception
lblFeedbackOK.Visible = False
lblFeedbackKO.Visible = True
Finally
'clear up memory
objWriter.Close()
msXml.Close()
End Try
End Sub
Protected Sub GenerateXml(ByVal mymessage As MailMessage, ByVal Name As String, ByVal Email As String, ByVal Subject As String, ByVal Body As String)
'create an instance of the XmlTextWriter object
objWriter = New XmlTextWriter(msXml, Encoding.UTF8)
objWriter.Formatting = Formatting.Indented
'start writing xml document
objWriter.WriteStartDocument()
'write a comment in our XML File
objWriter.WriteComment("Contact Form Output")
'starting with the root element
objWriter.WriteStartElement("ContactDetails")
'output the first contact detail element
objWriter.WriteStartElement("ContactDetail", Nothing)
objWriter.WriteElementString("Name", Name)
objWriter.WriteElementString("Email", Email)
objWriter.WriteElementString("Subject", Subject)
objWriter.WriteElementString("Body", Body)
objWriter.WriteElementString("Date", Date.Today())
'close contact detail element
objWriter.WriteEndElement()
'end contactdetails element
objWriter.WriteEndElement()
'flush and write XML data to file
objWriter.Flush()
msXml.Flush()
msXml.Position = 0
Dim att As New Attachment(msXml, DateTime.Now.ToFileTime.ToString() & ".xml")
mymessage.Attachments.Add(att)
End Sub
End Class
End Namespace
This is just a draft implementation. As they actually require a more detailed data collection form I will probably create a new contact form class so I am not duplicating the data passed back to the various methods.
Cheers
Retro