<style type="text/css">
.s4-specialNavLinkList
{
display:none !important;
}
</style>
.s4-specialNavLinkList
{
display:none !important;
}
</style>
WebPart, with some TODO comments. In the next section, we are going to replace the CreateChildControls() function and add some additional code to this class.CreateChildControls(). This function is where we will add labels, textboxes, and a button to allow the user to interact with our web part.CreateChildControls() function will allow us to reference these controls from the button event handler later on.TextBox txtContactName;
TextBox txtEmailAddress;
TextBox txtPhone;
TextBox txtMessage;
Button btnSendMessage;
Label lblMessageSent;
Now, let's add the code to CreateChildControls to build
the display. In order to keep it simple, an HTML table will be used to
align the controls in a consistent manner. Each table row will have two
cells: one for the field label, and the other for the text boxes.
Controls are created one at a time and added to a table cell, which is
then added to a table row.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);
}
Finally, we need to add an event handler to send the message as an
email when the Send Message button is clicked by the user. In the code
above, the event handler was already wired up with the line btnSendMessage.Click += new EventHandler btnSendMessage_Click);, so now we just need to create the btnSendMessage_Click
function. This function simply builds an email message using text that
was entered into the text boxes, and sends the email using SharePoint's SPUtility.SendEmail() function.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();
// TODO: Where to send the email
messageHeader.Add("to", "CustomerService@example.com");
// TODO: Who the email should be "from"
messageHeader.Add("from", "ContactForm@example.com");
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 = "";
}
btnSendMessage_Click().C:\>stsadm -o addsolution -filename ContactFormWebPart.wsp
C:\>stsadm -o deploysolution -name ContactFormWebPart.wsp -immediate
-allowgacdeployment -allcontenturls
Now, the web part is available for use on the production SharePoint
server. Only one more thing is left to do. When you want to use it in a
SharePoint site, you will need to add it to that site's web part
gallery. To do this, navigate to your SharePoint site and go to Site
Actions - Site Settings. Then, click on the Web Parts link under the
Galleries section. This opens up a web page listing the web parts that
are currently available for this site. Click New to add a new web part
to the list. The next web page shows the deployed web parts. Check the
box next to ContactFormWebPart.ContactForm, and then click the Populate Gallery button.