Tags

,

You can make simple forms with Sitecore without incorporating the Web Forms For Marketers module. Although WFFM is an fine module, it is still a module. Styling, configuration, and extensions of modules often result in the module being an obstacle rather than a help. Making your own stuff gives you complete control over the code and Sitecore items. So make your own stuff as you need it, this post is for inspiration.

You start bij adding a Form template. This could contain the text for the Send-button, and the e-mail addresses that the form uses. Because sending mails is the main activity of the form. Then we add a template for the fieldsets. These will be the building blocks of the form. It should contain the labeltext (Single-Line Text), IsRequired (Checkbox) and FieldType (Droplist). The FieldType is the most interesting, because this droplist will be filled with MetaData-items. So the last template we need is the FieldType, which only a textfield named ‘FieldType’. Configure the __Standard Values of Form so that the only insert option is a FieldSet.

Form Template

Then add a few content items based on the template FieldType: TextBox, TextArea, and CheckBox.

FieldTypes

And add a content item based on the template of the Form itself. Add a few fieldsets, fill in label texts, and choose one of the FieldTypes that you have defined from the droplist FieldType. The fieldset ‘I want to receive newsletter’ should be a CheckBox, the rest should be TextBox.

ContactForm

Now add some simple code to show the form on the website: a FieldRenderer for the title, a Button, and a Repeater for the fieldsets.

<h2><sc:FieldRenderer ID="FieldRendererTitle" runat="server" FieldName="FormTitle" />

<asp:Repeater ID="RepeaterFieldSets" runat="server">
	<ItemTemplate>
		<fieldset>
			<asp:PlaceHolder ID="PlaceHolderFieldSet" runat="server">
				<asp:Label ID="LabelField" runat="server" />
			</asp:PlaceHolder>
		</fieldset>
	</ItemTemplate>
</asp:Repeater>

<asp:Button ID="ButtonSend" runat="server" />

The Repeater’s Datasource will be set by a repository, so we get into that code in a moment. First make sure the content is set:

private void SetContent()
{
	FieldRendererTitle.DataSource = DataSource;

	ButtonSend.Text = Item.Fields["SendButtonText"].Value;

	RepeaterFieldSets.DataSource = _contactFormFieldSetItemRepository.GetFormFieldsByContactFormName ("Contactformulier");
	RepeaterFieldSets.DataBind();
}

private void RepeaterFieldSets_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
	var item = (ContactFormFieldSetItem)e.Item.DataItem;
	var placeHolder = (PlaceHolder) e.Item.FindControl("PlaceHolderFieldSet");
	var label = (Label)placeHolder.FindControl("LabelField");

	label.Text = item.LabelText;
	placeHolder.Controls.Add(item.FieldType);
}

Now we need the code that makes ASP.NET controls from the information retrieved from Sitecore. The repository gets this information from the Sitecore database:

public class ContactFormFieldSetItemRepository
{
	public List<IContactFormFieldSetItem> GetFormFieldsByContactFormName(string itemName)
	{
		Item item = global::Sitecore.Context.Database.GetItem("/sitecore/content/Global/Standard Items/Contact/" + itemName);

		var contactFormFieldSetItemList = new List<IContactFormFieldSetItem>();

		foreach (var child in item.Children)
		{
			ContactFormFieldSetItem contactFormFieldSetItem = new ContactFormFieldSetItem();

			contactFormFieldSetItem.SetInnerItem(child as Item);

			contactFormFieldSetItemList.Add(contactFormFieldSetItem);
		}
		return contactFormFieldSetItemList;
	}
}

The repository returns a list of ContactFormFieldSetItems, so have to define this as well. We make a model that fills itself with the fields of the Sitecore item. You should come up yourself with the exact logic for this, because otherwise this post will become way too long! The bottomline is that the model has properties with only a getter that returns a Fields[“myField”].Value. This is how the property FieldType is determined:

public class ContactFormFieldSetItem : SitecoreItem, IContactFormFieldSetItem
{
	public WebControl FieldType
	{
		get
		{
			if (_innerItem.Fields["Contact_ContactFormFieldItem_FieldType"] != null)
			{
				switch (_innerItem.Fields["Contact_ContactFormFieldItem_FieldType"].Value)
				{
					case "TextBox" :
						TextBox textBox = new TextBox();
						return textBox;
						case "TextArea" :
						TextBox textArea = new TextBox();
						textArea.TextMode = TextBoxMode.MultiLine;
						return textArea;
					case "CheckBox":
						CheckBox checkBox = new CheckBox();
						return checkBox;
					default :
						return new TextBox();
				}
			}
			else
			{
				return new TextBox();
			}
		}
	}
}

Because the switch only looks at a string, it is important that the end user does not have access to the MetaData of the FieldType items. He should interact with the FieldType in Sitecore only with the droplist. That way nothing can go wrong.

Now it the most important thing the form should do is send an e-mail when a visitor fills in the form. So we iterate through the fieldsets in the Repeater, and read the values of these form fields. These values will be added in the e-mail. You have to add the logic for adding the RequiredFieldValidators when the added FieldSet is required yourself, because this post is getting too long. But it is not that hard. This form is really simple, but it saves you importing a module. You can also easily extend the forms functionality, because it your code. You can add a new FieldType in Sitecore, for example E-mail. Then in the following handler you can read the value of the E-mail field, and send an e-mail to the address the visitor has filled in.

private void ButtonSend_Click(object sender, EventArgs e)
{
	StringBuilder mailMessageBody = new StringBuilder();
	mailMessageBody.Append("Dear Sir/Madam, <br /><br />");
	mailMessageBody.Append("You have filled in the following on OurWebsite.com: <br /><br />");

	foreach (RepeaterItem repeaterItem in RepeaterFieldSets.Items)
	{
		foreach (var control in repeaterItem.FindControl("PlaceHolderFieldSet").Controls)
		{
			if (control is Label)
			{
				Label label = control as Label;
				mailMessageBody.Append(label.Text + ": ");
			}
			if (control is TextBox)
			{
				TextBox textbox = control as TextBox;
				mailMessageBody.Append(textbox.Text + "<br />");
			}
			if (control is CheckBox)
			{
				CheckBox checkBox = control as CheckBox;
				mailMessageBody.Append(checkBox.Checked + "<br />");
			}
		}
	}
	mailMessageBody.Append("<br /><br />Sincerely,<br /><br />OurWebsite Team");

	MailMessage message = new MailMessage("noreply@our-website.com", "contact@ourwebsite.com");
	message.Subject = "NOTIFICATION: contact form";
	message.Body = mailMessageBody.ToString();
	message.IsBodyHtml = true;

	SmtpClient smtpClient = new SmtpClient("smtpclient.our-website.com");
	smtpClient.Send(message);
}

Advertisements