Create form responses programmatically? - General Discussions - General Discussions - Progress Community
 General Discussions

Create form responses programmatically?

  • 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();
        
     

    Where "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.

    I hope you find this useful.

    Regards,
    Randy Hodge
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  •    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();
                        


    Regards,
    Ivan D. Dimitrov
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  • 68c1521e-37c1-6deb-a958-ff0000446526_not_accessible.png

    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"