Creating a Contact Form
In this tutorial, we'll look at creating a Contact Form using Umbraco Forms. It will take you through the process of creating a Contact Form and cover all the different components involved in building the form.
You can use a Contact Form on your website to allow a visitors to send you a message. Having a Contact Form on your website allows you to keep track of potential customer queries and possibly generate leads via email communication.
Video Tutorial
Step 1: Configure the Document Types
The first step in this tutorial is to configure the Document Types that will be used to show the Contact Form on your website.
Creating a Composition
We'll start off by creating a Composition. A Composition is a stand-alone Document Type, that you can reuse on other Document Types. By creating a Composition, we are not duplicating the same properties on multiple Document Types. This is helpful when we want to use the same set of properties on multiple Document Types.
To create a Composition, follow these steps:
Go to Settings in the Umbraco Backoffice.
Expand the Document Types folder in the Settings tree.
Select ... next to the Compositions folder.
Click Create.
Select Document Type.
Enter a Name for the Composition- let's call it Title Box.
Add the following fields with the respective specifications:
GroupField NameAliasData TypeTitle Box
Title
title
Textstring
Title Box
Subtitle
subtitle
Textarea
Click Save to save the Composition.
Creating a Contact Us Document Type with Template
Next, we will create a Document type with template. A Document Type contains different properties for holding different types of content. The Document Type we create here will be the one used for creating the content page that will hold our Contact Form.
To create a Contact Us Document Type, follow these steps:
Go to Settings in the Umbraco Backoffice.
Select ... next to the Document Types folder.
Click Create.
Select Document Type with Template.
Enter a Name for the Document Type- let's call it Contact Us.
Select Compositions in the top-right corner.
Select Title Box.
Click Submit.
Add the following fields with the respective specifications:
GroupField NameAliasData TypeForm
Contact Form
contactForm
Form Picker
Content
Body Text
bodyText
Richtext Editor
Click Save.
Updating the Document Type Permission
In the following we will update the Document Type permissions to specifically add child nodes under the root content node.
To update the Contact Us Document Type permissions, follow these steps:
Navigate to the Document Type used for the root content node on your website, in this case Home page.
Go to the Structure tab.
Select Choose in the Allowed child node types section.
Select the Contact Us page.
Click Choose.
Click Save.
Step 2: Prepare the Content Node
This step takes you through creating the content node for your Contact Form. The content node uses the Document Type and Template to serve up an HTML page to web visitors.
To add the content node, follow these steps:
Go to Content in the Umbraco Backoffice.
Select ... next to the Home Page.
Click Create.
Select Contact Us.
Enter the name for the content node. let's call it Contact Us.
Enter a Title, Subtitle, and Body Text value. These can always be updated at a later point.
Click Save or Save and Publish.
Step 3: Creating the Contact Form
In this step, we will create the Contact Form using Umbraco Forms.
To create a form, follow these steps:
Go to the Forms section in the Umbraco Backoffice.
Click ... next to the Forms folder.
Click Create.
Select New Form....
Enter a Name for the Form. Let's call it Contact Us.
[Optional] Enter a Page Name and Group Name for the Data Consent statement. Let's call it Data Consent.
Click Add new group. Let's call it Information.
Select Add Question to add a new field.
Enter the following details:
Field NameValueEnter question
Name
Alias
fullName
Choose answer type
Short answer
Field Type
text
Mandatory
On
Click Submit.
Repeat steps 8-10 to add the following fields:
Field NameValueEnter question
Company Name
Choose answer type
Short answer
Field NameValueEnter question
How should we contact you?
Choose answer type
Single choice
Prevalues Items
phone, email
Mandatory
On
Field NameValueEnter question
Enter your phone number
Choose answer type
Short answer
Field Type
tel
Validation
Validate as a number
Enable Conditions in the Enter your phone number field.
Click Add Condition.
Select How should we contact you? from the dropwdown.
Select phone in the value field.
Click Submit.
Repeat steps 8-10 to add the following field:
Field NameValueEnter question
Enter your email address
Choose answer type
Short answer
Field Type
email
Validation
Validate as an email address
Enable Conditions in the Enter your email address field.
Click Add Condition.
Select How should we contact you? from the dropwdown.
Select email in the value field.
Click Submit.
Repeat steps 8-10 to add the following field:
Field NameValueEnter question
What is your role?
Choose answer type
Dropdown
Prevalues Items
manager, developer, tester, writer, marketing specialist
Field NameValueEnter question
Attachments (if any)
Choose answer type
File upload
Predefined allowed file types
pdf, png, jpg, gif, txt
Field NameValueEnter question
Are you a Robot?
Choose answer type
reCAPTCHAv2
Theme
light
Size
normal
Mandatory
On
Select the Reorder option.
Drag the Data consent group below the Information group.
Click I am done reordering.
Click Save.
Configuring the Form Workflow
Workflows is how you determine what you happen after your form is submitted. It could be actions like sending an email or displaying a "Thank You" message.
To configure the Form workflow, follow these steps:
Select the Submit message/ Go to page options in the bottom of the Forms editor.
Enter a customised message in the Message on Submit field.
Click Submit.
Click on Send template email to xxx@xx.dk.
Enter an email address in the Sender Email field.
By default, the Example-Template.cshtml template will be selected in the Email template field.
Enable Attach Uploaded Files.
Click Submit.
Click Save.
Configuring the Form Settings
In this step, you will find the information about accessing the Forms Settings and the validations available to customise your form.
To configure the form settings, follow these steps:
Navigate to the Settings tab in the Forms editor.
Scroll to find the Validation section.
Ensure that the Mark Mandatory fields option is checked under Mark fields.
Click Save.
There are multiple settings that be configured. These are all optional in relation to this tutorial.
Step 4: Adding the Contact Form to the Content Node
Now that you have created your Contact Form, you can add it in the Contact Us Content Node using the Form Picke Data Type.
To add the Contact Form to the Content Node, follow these steps:
Go to the Content section in the Umbraco Backoffice.
Open the Contact Us Page.
Select Choose in the Contact Form field.
Select the Insert Form with Theme option.
Select the Contact Us Form.
Click Choose.
Click Save or Save and Publish.
Step 5: Additional configuration
In the next couple of steps, we will add some additional configuration required in order for our form to work properly.
Configuring the reCAPTCHA value
You need to update the configuration to include a value in the appsettings.json
file.
To configure the reCAPTCHA value, see the reCAPTCHA configuration article.
Configuring Simple Mail Transfer Protocol (SMTP)
By adding the SMTP settings in the appsettings.json
file, you can send out emails from your Umbraco installation. It is required in order for your form to be able to send emails on submission.
To configure the SMTP settings, see the Global Settings article.
Step 6: Rendering the Contact Form
In this step, we will render the values of the Contact Us Document Type in the template.
To render the Contact Form, follow these steps:
Go to the Settings section in the Umbraco Backoffice.
Open the Contact Us template in the Templates folder.
Enter the following code to render the form:
Select Insert.
Click Value.
Select Document Type from the Choose field dropdown.
Select Contact Us.
Click Choose.
Select bodyText from the Contact Us dropdown.
Click Submit.
Click Save.
For Umbraco Forms to work correctly, you need to include some client dependencies. For more information, see the Preparing Your Frontend article.
The final result
Finally, it is time to view the Contact Form on the frontend.
To view the Contact Form on the Frontend, follow these steps:
Go to the Content section in the Umbraco Backoffice.
Open the Contact Us Page.
Ensure that the page is published.
Go to the Info tab.
Click on the Published link in the Links section.
You now have a full-fledged Contact Form ready to be used on your website.
Last updated