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 label FirstName, for example. For more information about creating forms programmatically, see For developers: Create forms.

To create form responses:

  1. Create a new web form and in the code-behind of the web form, get an instance of the FormsManager.
  2. Using the web form's name, get the web form that requires responses.
  3. Create a new form entry of type FormEntry.
  4. Set the value of the textbox, the IP address, the time when the response is submitted, and user ID.
  5. Set the language of the entry.
  6. To increase the form entry seed and set the referral code.
  7. Call the SaveChanges method 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.
New to Sitefinity?