If you've started working with Form's in Sitefinity 4.0, you probably already know that your response data is persisted for you in your Sitefinity database in a table the Sitefinity engine created for you automatically. Let's suppose you'd like to get access to that data in your code-behind to use it somewhere else in your website.
The first step in this process is to determine the form name and your form widget's underlying fieldname. For more information on setting the names of the columns or finding the table in your database where the form's response data is stored, you should review another excellent post from Gabe Sumner at Sitefinity.
Keeping it simple, let's suppose we have a new form called "TestForm" and keep the default developer name for the form ("sf_testform").
Next, add a TextBox form widget to your form and set the "Advanced name for developers" to "newtextbox".
Make sure you "Publish" the form. Before going any further, I recommend examining the database and finding your new table and column name to be sure they have the names you expect. The reason is I've seen the Sitefinity engine change the names slightly by adding underscores between capital letters, so verifying the generated names may save you some time when debugging. I used the MS SQL Server Management Studio to see the new table:
My names look good, as you can see the table name is "sf_testform" and the textbox's column name is "newtextbox".
For testing purposes, we need to create some sample data. To keep things simple, we'll create our responses in the Sitefinity Administrator by going to the Forms Content and clicking "responses" for my new form.
I created three responses to verify I can see that data in my code:
Finally, now that we have completed all the work to get some sample data stored in our DB and verified the appropriate form and column names, here is the code to access that data programmatically. (Hint: be sure to include the following using statement in your code to get access the libraries necessary for this code: using Telerik.Sitefinity.Model;)
FormsManager formsManager = FormsManager.GetManager();
// Get the form named "sf_testform"
var forms = formsManager.GetForms().Where(f => f.Name == "sf_testform");
foreach (var form in forms)
{
var records = formsManager.GetFormEntries(form);
// Loop through all the response records for this form
foreach (var record in records)
{
FormEntry fe = (FormEntry)record;
// Grab the newtextbox column value to display in a list box
var TextBoxValue = fe.GetValue("newtextbox");
ListBox1.Items.Add(new ListItem() { Text = TextBoxValue.ToString() });
}
}
As you can see, this sample code is using the FormsManager to get an instance of our Form. It is passing the form instance to the GetFformEntries method to get the response data, for each FormEntry in the collection it is getting the value of our specific column and putting it into a ListBox.
I’ve kept things very simple to illustrate the basics, so hopefully this will be a good starting point for you to expand on in your own website.