Utilizing forms to collect information from visitors in a streamlined, flexible manner is a core component for most websites (e.g., “Contact Us” form). Similarly, easily creating and maintaining these forms without interaction from the development team, as well as viewing submitted form data are equally important aspects of a modern website. While Episerver does not supply this functionality out of the box, the Episerver Forms add-on satisfies each of these requirements.
The Forms add-on is free, and can be installed via Nuget:
It allows editors to create custom forms using a variety of built-in form elements, and is also extensible through custom elements. These forms, and their elements, are based on the concept of blocks, which means they can then be used throughout a site, just like any other block. Form submissions can easily be viewed and sorted via the Episerver backend. Additionally, it’s possible to configure post-submission emails, as well as custom “WebHook actors,” which can be used to facilitate third-party integrations.
Functionally speaking, Episerver Forms works well, and being based on the concept of blocks means that it offers editors an easy to use, familiar method to create custom forms. Overall, the editor experience is good – including the user guide.
However, things aren’t as clear from a development perspective; this became apparent during a recent project when attempting to create forms via code rather than the backend. The developer documentation is not as robust as the editor-focused user guide, which means doing CRUD operations involves some detective work (via decompiled DLLs), along with some good, old-fashioned trial and error.
As mentioned, Episerver Forms are block-based, which means they can be created and modified the same way as any other piece of IContent
– the ubiquitous IContentRepository
saves the day! This interface provides basic CRUD operations for anything that implements IContent
, and is likely already familiar to anyone that has worked with the Episerver API.
An Episerver Form is essentially a block that contains several properties, including a ContentArea
that holds a collection of form elements. The form itself inherits from FormContainerBlock
, while each form element inherits from ElementBlockBase
. These classes are not well documented, but are reasonably easy to understand. As a side note: it is possible to extend these classes with custom implementations (as well as using a custom view), but the default types will be used in the following examples.
Anyway, enough background information – without further ado, some let’s get into some code.
To create a new form via the API:
This is a relatively standard example of using IContentRepository
to create a new piece of IContent
. The only differences are a few form-specific properties:
Once a form has been created, form elements can be added to the ElementsArea
property. However, before elements can be added to this ContentArea
, they must first be created.
To create a new form element via the API:
Again, this is relatively straightforward, but not well documented. The element-specific properties are as follows:
One thing to note is that the form elements get created in the form’s asset folder (“For This Page/Block” in the backend), which makes them available to only that form. It may be possible to create form elements elsewhere, and share them across multiple forms, but this has not been tested. Also, note that the IContent.Name
property is required — it is used to render the column header when viewing the form submission data via the Episerver backend.
There are a variety of built-in form element types that cover the most common input types, and custom elements can also be created for use cases that are not covered. The built-in element types are named in a self-explanatory manner (e.g., TextboxElementBlock
), and exist in the EpiServer.Forms.Implementation.Elements
namespace.
As noted above, the Validators
property on the form element block is responsible for element value validation. This property is a string, and contains a delimited collection of fully-namespaced validator types. This delimiter is not documented, but some testing reveals that it’s a triple pipe (“|||”). There also appears to be a constant for this value at EPiServer.Forms.Constants.RecordSeparator
.
For example, to set the Validators
property for a required email form element:
The built-in validators exist in the EpiServer.Forms.Implementation.Validation
namespace and, like the built-in form elements, are named in a self-explanatory manner (e.g., RequiredValidator
). It’s also possible to create custom validators. The official documentation provides and example of how to leverage a custom validator.
To add an existing element to an existing form:
No surprises here – just add the element and save the form!
Offering visitors a method to submit data via forms is is a core component in a modern website. This collection of visitor data, which can range from a simple “Contact Us” form to complex, multi-step forms, provides a direct channel to potential customers. Overall, the Episerver Forms add-on offers a flexible, easy to use method for editors to facilitate this data collection without involvement from the development team.
Though the editor experience is relatively streamlined and well-documented, developers may need to do some investigative work in order to build and maintain forms via the API. The developer documentation contains an overview of the functionality offered by Forms, but the lack of code-based examples makes interacting with the API somewhat difficult. For example, the specifics in this post (e.g., EPiServer.Forms.Constants.RecordSeparator
) were discovered through some quality time with dotPeek.
Did I miss something? Probably. Let me know in the comments!