How to create form responses programmatically for Sitefinity 9.0+

Since Sitefinity 9.0, the FormEntriesSeed property is deprecated as noted in the API breaking changes. This blog post will provide the correct way to submit a form entry using the latest API.

For versions below Sitefinity 9.0, check official Sitefinity documentation to create form responses.

For Sitefinity 9.0+ use the code below:

            Dictionary<string, string> inputs = new Dictionary<string, string>();

            //retrieves the values of page controls and stores them in a custom collection inputs
            inputs.Add("FirstName", this.txtName.Text);
            inputs.Add("Surname", this.txtSurname.Text);
            inputs.Add("Phone", this.txtPhone.Text);
            inputs.Add("Email", this.txtEmail.Text);
            inputs.Add("Message", this.txtMessage.Text);

            FormsManager manager = FormsManager.GetManager();
            var form = manager.GetFormByName("sf_contactus");
            FormEntry entry = manager.CreateFormEntry(form.EntriesTypeName);

            foreach (var item in inputs)
            {
                entry.SetValue(item.Key, item.Value);
            }

            entry.IpAddress = this.Page.Request.UserHostAddress;
            var identity = ClaimsManager.GetCurrentIdentity();
            entry.UserId = identity != null ? identity.UserId : Guid.Empty;

            if (AppSettings.CurrentSettings.Multilingual)
            {
                entry.Language = System.Globalization.CultureInfo.CurrentUICulture.Name;
            }

            entry.ReferralCode = (manager.Provider.GetNextReferralCode(entry.GetType().ToString())).ToString();
            entry.SubmittedOn = System.DateTime.UtcNow;

            manager.SaveChanges();

Note the usage of GetNextRefferalCode method, part of the FormManager's Provider class. The method uses the entry type and returns proper referral code.

Veronica Milcheva

About Veronica Milcheva

I am a passionate Sitefinity blogger, developer and consultant. In my spare time I enjoy running and listening to music. My personal quote: There's no tough problem, just not enough coffee :)

View Comments

comments powered by Disqus