Create form responses programmatically?
Can anyone provide an example of how to do this? I'm an experienced programmer, but I'm not very familiar with the Sitefinity framework / data model.
Hello Raymond,
Here is a sample that will create a form response.
void
GenerateFormResponse()
FormsManager formsMgr = FormsManager.GetManager();
formsMgr.Provider.SuppressSecurityChecks =
true
;
var form = formsMgr.GetFormByName(
"sf_testform1"
);
if
(form !=
null
)
FormEntry entry = formsMgr.CreateFormEntry(String.Format(
"0.1"
, formsMgr.Provider.FormsNamespace, form.Name));
entry.SetValue(
"FormTextBox_C001"
,
"Angus"
); //first name
entry.SetValue(
"FormTextBox_C003"
,
"Thermopile"
); //last name
entry.IpAddress = Page.Request.UserHostAddress;
entry.SubmittedOn = DateTime.UtcNow;
entry.UserId = ClaimsManager.GetCurrentUserId();
if
(SystemManager.CurrentContext.AppSettings.Multilingual)
entry.Language = CultureInfo.CurrentUICulture.Name;
form.FormEntriesSeed++;
entry.ReferralCode = form.FormEntriesSeed.ToString();
formsMgr.SaveChanges();
"sf_testform1"
is the name of your form which you can find under the "Advanced" section of the form designer and "FormTextBox_C001"
is the name of the first form control in your form which you set under the "Advanced' section in the form control designer.You can also try this
using
(var fluent = App.WorkWith())
fluent.Forms()
.Form(
"sf_empdetails"
)
.Entry()
.CreateNew(System.Guid.NewGuid())
.SetFieldValue(
"FormTextBox_C001"
, name)
.SetFieldValue(
"FormTextBox_C002"
, name2)
.SaveChanges();
I wonder how to add other values to the entry with fluent api like
entry.UserId
Hi all,
@Paula
You can use the .Do extension to add the UserId property:
using
(var fluent = App.WorkWith())
fluent.Forms()
.Form(
"sf_YourForm"
)
.Entry()
.CreateNew(System.Guid.NewGuid())
.Do(a => a.UserId = theUserId)
.SetFieldValue(
"FormTextBox_C001"
,
"Value1"
)
.SaveChanges();
Hi,
I am facing issue with "SetValue" method of FormEntry object, while I am trying to access "SetValue" method with FormEntry object. I am unable to get the same. Is there any sitefinity version issue or anything else. I am using Sitefinity V.8.0. Please find the attachment.
entry.SetValue("FormTextBox_C001", "Angus"); //first name
Thanks
I think you're missing an import, "Telerik.Sitefinity.Model"