Create form responses
You can create multiple responses for a specific web form to analyze web form analytics and to monitor web form metrics, for example. You can create the required number of form responses all at once in the web form's code-behind.
PREREQUISITES: You created a web form with name
testform1, for example, and dragged into the content of the form a textbox with labelFirstName, for example. For more information about creating forms programmatically, see For developers: Create forms.
To create form responses:
- Create a new web form and in the code-behind of the web form, get an instance of the
FormsManager. - Using the web form's name, get the web form that requires responses.
- Create a new form entry of type
FormEntry. - Set the value of the textbox, the IP address, the time when the response is submitted, and user ID.
- Set the language of the entry.
- To increase the form entry seed and set the referral code.
- Call the
SaveChangesmethod to preserve the changes to the database.
The following code illustrates a implementation of the web form's code-behind:
C#
using System;
using Telerik.Sitefinity.Forms.Model;
using Telerik.Sitefinity.Modules.Forms;
using Telerik.Sitefinity.Security.Claims;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Model;
namespace SitefinityWebApp
{
public partial class CreateFormResponce
{
public static void CreateFormResponses()
{
//get an instance of the FormsManager
FormsManager formsManager = FormsManager.GetManager();
//retrieve the form by name
var form = formsManager.GetFormByName("sf_testform1");
if (form != null)
{
var entryTypeName = String.Format("{0}.{1}", formsManager.Provider.FormsNamespace, form.Name);
FormEntry entry = formsManager.CreateFormEntry(entryTypeName);
//set value to the text box control with title FirstName
entry.SetValue("FormTextBox_C001", "FirstName");
//set the response properties as UserId, IPAddress, the datе when the response is submitted
entry.IpAddress = SystemManager.CurrentHttpContext.Request.UserHostAddress;
entry.SubmittedOn = DateTime.UtcNow;
entry.UserId = ClaimsManager.GetCurrentUserId();
//set the response language if multisite environment
entry.Language = SystemManager.CurrentContext.Culture.Name;
//set the referral code
entry.ReferralCode = formsManager.Provider.GetNextReferralCode(entryTypeName).ToString();
//always call save changes to preserve to database
formsManager.SaveChanges();
}
}
}
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
Foundations of Sitefinity ASP.NET Core Development
The free on-demand video course teaches developers how to use Sitefinity ASP.NET Core and take advantage of its decoupled architecture and modern development model.