Create forms
Form controls
To add controls programmatically to a form, you use the Telerik.Sitefinity.Modules.Forms.Web.UI.Fields namespace that contains all form controls. The form control classes are listed below:
- FormTextBox
- FormMultipleChoice
- FormCheckboxes
- FormParagraphTextBox
- FormDropDownList
- FormSectionHeader
- FormInstructionalText
- FormSubmitButton
Creating a form
To create a form, you must use the FormsManager class. In this example you create a form with a field for entering the name and aSubmit button. You use FormTextBox for the name text field and FormSubmitButton for the Submit button.
To create the form, perform the following:
-
Create a form in Sitefinity CMS backend by pasting the following code: ```C# using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Web.UI; using Telerik.Sitefinity.Forms.Model; using Telerik.Sitefinity.Modules.Forms;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Forms { public partial class FormsSnippets { public static void CreateForm(Guid formId, string formName, string formTitle, string formSuccessMessage, Dictionary<Control, string> formControls) { var formManager = FormsManager.GetManager(); var form = formManager.GetForms().Where(f => f.Id == formId).SingleOrDefault(); Guid siblingId = Guid.Empty;
if (form == null) { form = formManager.CreateForm(formName, formId); form.Title = formTitle; form.UrlName = Regex.Replace(form.Name.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-"); form.SuccessMessage = formSuccessMessage; var draft = formManager.EditForm(form.Id); var master = formManager.Lifecycle.CheckOut(draft); if (master != null) { if (formControls != null && formControls.Count > 0) { int controlsCounter = 0; foreach (var control in formControls) { control.Key.ID = string.Format(formName + "_C" + controlsCounter.ToString().PadLeft(3, '0')); controlsCounter++; var formControl = formManager.CreateControl<FormDraftControl>(control.Key, control.Value); formControl.SiblingId = siblingId; formControl.Caption = control.Key.GetType().Name; siblingId = formControl.Id; master.Controls.Add(formControl); } } master = formManager.Lifecycle.CheckIn(master); formManager.Lifecycle.Publish(master); formManager.SaveChanges(); } } } }}
-
Create the controls for the example form and call the CreateForm method by pasting the following code: ```C# using System; using System.Collections.Generic; using System.Linq; using System.Web.UI; using Telerik.Sitefinity.Modules.Forms; using Telerik.Sitefinity.Modules.Forms.Web.UI.Fields;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Forms { public partial class FormsSnippets { private void CreateContactForm(Guid contactFormId) { var formManager = FormsManager.GetManager(); var form = formManager.GetForms().Where(f => f.Id == contactFormId).SingleOrDefault();
if (form == null) { Dictionary<Control, string> controls = new Dictionary<Control, string>(); string name = "Contact"; string title = "Contact Form"; string successfulMessage = "Success! Thanks for filling out our form!"; FormTextBox nameBox = new FormTextBox(); nameBox.Title = "Your name"; nameBox.Description = "Enter your name. Must be between 5 and 30 characters."; controls.Add(nameBox, "Body"); FormSubmitButton submitButton = new FormSubmitButton(); submitButton.Text = "Submit"; controls.Add(submitButton, "Body"); CreateForm(contactFormId, name, title, successfulMessage, controls); } } }}
In CreateForm, first, you get the FormsManager and verify that the form is not yet created. You edit and check out the form. Then, you add the controls to the form draft by wrapping them in FormDraftControl. You use siblingId to keep the last control in the placeholder where the current control must be placed in. Finally, you check in and publish the form.
In CreateContactForm, first, you set the name, title and success message for the form. Then, you create the name field and the Submitbutton. Once created, you add the controls in a dictionary with the name of the placeholder where they must be placed in. Finally, you callCreateForm to create the form in Sitefinity CMS backend.