Click here to Skip to main content
15,888,323 members
Articles / Web Development / HTML

Creating a Contact Form Web Part for SharePoint

Rate me:
Please Sign up or sign in to vote.
4.96/5 (19 votes)
8 Dec 2008CPOL10 min read 555.2K   2.2K   67  
An article that introduces SharePoint web part development by creating a simple contact form web part.
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace ContactFormWebPart
{
    [Guid("b4823966-27fe-40b4-8d47-e6ae66a1c15f")]
    public class ContactForm : System.Web.UI.WebControls.WebParts.WebPart
    {
        TextBox txtContactName;
        TextBox txtEmailAddress;
        TextBox txtPhone;
        TextBox txtMessage;
        Button btnSendMessage;
        Label lblMessageSent;

        public ContactForm()
        {
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Table t;
            TableRow tr;
            TableCell tc;

            // A table that is used to layout the controls
            t = new Table();

            // Label with instructions for the user
            tr = new TableRow();
            tc = new TableCell();
            tc.ColumnSpan = 2;
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblInstructions = new Label();
            lblInstructions.Text = "Please enter your contact information and message below.";
            tc.Controls.Add(lblInstructions);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Contact Name label
            tr = new TableRow();
            tc = new TableCell();
            tc.Style["padding-top"] = "7px";
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblContactName = new Label();
            lblContactName.Text = "Name:";
            tc.Controls.Add(lblContactName);
            tr.Controls.Add(tc);

            // Contact Name textbox
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            txtContactName = new TextBox();
            txtContactName.ID = "txtContactName";
            txtContactName.Width = Unit.Pixel(300);
            tc.Controls.Add(txtContactName);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Email Address label
            tr = new TableRow();
            tc = new TableCell();
            tc.Style["padding-top"] = "7px";
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblEmailAddress = new Label();
            lblEmailAddress.Text = "Email Address:";
            tc.Controls.Add(lblEmailAddress);
            tr.Controls.Add(tc);

            // Email Address textbox
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            txtEmailAddress = new TextBox();
            txtEmailAddress.ID = "txtEmailAddress";
            txtEmailAddress.Width = Unit.Pixel(300);
            tc.Controls.Add(txtEmailAddress);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Phone Number label
            tr = new TableRow();
            tc = new TableCell();
            tc.Style["padding-top"] = "7px";
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblPhone = new Label();
            lblPhone.Text = "Phone Number:";
            tc.Controls.Add(lblPhone);
            tr.Controls.Add(tc);

            // Phone Number textbox
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            txtPhone = new TextBox();
            txtPhone.ID = "txtPhone";
            txtPhone.Width = Unit.Pixel(300);
            tc.Controls.Add(txtPhone);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Message label
            tr = new TableRow();
            tc = new TableCell();
            tc.Style["padding-top"] = "7px";
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblMessage = new Label();
            lblMessage.Text = "Message:";
            tc.Controls.Add(lblMessage);
            tr.Controls.Add(tc);

            // Message textbox
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            txtMessage = new TextBox();
            txtMessage.ID = "txtMessage";
            txtMessage.Height = Unit.Pixel(100);
            txtMessage.Width = Unit.Pixel(400);
            txtMessage.TextMode = TextBoxMode.MultiLine;
            txtMessage.Wrap = true;
            tc.Controls.Add(txtMessage);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Empty table cell
            tr = new TableRow();
            tc = new TableCell();
            tr.Controls.Add(tc);

            // Label for telling the user the message was sent
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            lblMessageSent = new Label();
            lblMessageSent.Text = "Your message has been sent.  Thank you.";
            lblMessageSent.Font.Bold = true;
            lblMessageSent.Visible = false;
            tc.Controls.Add(lblMessageSent);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Empty table cell
            tr = new TableRow();
            tc = new TableCell();
            tr.Controls.Add(tc);

            // Send Message button
            tc = new TableCell();
            btnSendMessage = new Button();
            btnSendMessage.Text = "Send Message";
            btnSendMessage.Click += new EventHandler(btnSendMessage_Click);
            tc.Controls.Add(btnSendMessage);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            this.Controls.Add(t);
        }

        protected void btnSendMessage_Click(object sender, EventArgs e)
        {
            // Build the email subject string
            System.Text.StringBuilder subject = new System.Text.StringBuilder();
            subject.Append("Contact Form Message from ");
            subject.Append(txtContactName.Text);

            // Build the email message string
            System.Text.StringBuilder message = new System.Text.StringBuilder();
            message.Append("Contact Name: ");
            message.AppendLine(txtContactName.Text);
            message.Append("Email Address: ");
            message.AppendLine(txtEmailAddress.Text);
            message.Append("Phone: ");
            message.AppendLine(txtPhone.Text);
            message.AppendLine();
            message.AppendLine("Message:");
            message.AppendLine(txtMessage.Text);

            // Construct the message header
            System.Collections.Specialized.StringDictionary messageHeader =
            new System.Collections.Specialized.StringDictionary();
            messageHeader.Add("to", "CustomerService@example.com");   // TODO: Where to send the email
            messageHeader.Add("from", "ContactForm@example.com");     // TODO: Who the email should be "from"
            messageHeader.Add("subject", subject.ToString());
            messageHeader.Add("content-type", "text/plain");

            // Send the email
            Microsoft.SharePoint.Utilities.SPUtility.SendEmail(
            SPContext.Current.Web, messageHeader, message.ToString());

            // Let the user know the message was sent
            lblMessageSent.Visible = true;

            // Clear out the input fields
            txtContactName.Text = "";
            txtEmailAddress.Text = "";
            txtPhone.Text = "";
            txtMessage.Text = "";
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Cinlogic LLC
United States United States
Software development consultant in Cincinnati, Ohio.

Comments and Discussions