Related Tags:
C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, CMS A content management system (CMS) is a software application or set of related programs that are used to create and manage digital content. CMSes are typically used for enterprise content management (ECM) and web content management (WCM) Learn More, Sitecore Sitecore is one of the leading enterprise-level content management systems built on ASP.NET, enabling web content editors and marketers to have full control over all aspects of their website from social integration and blog posts to advanced personalisation, e-commerce and more. Launched in 2001, Sitecore has used the .NET platform from the beginning of the language itself, and has been growing in popularity over the last few years. Currently on its 7th major version, it now runs on .NET 2.0/4.0, and the core has been rewritten from scratch to take advantage of the improvements made in ASP.NET 4.5. Learn More, Sitecore 9 Sitecore is a Web Content Management System (CMS) built on Microsoft ASP.net. This tag is for sitecore version 9. Learn More, Sitecore 10 Sitecore is a Web Content Management System (CMS) built on Microsoft ASP.net. This tag is for sitecore version 10.0 Learn More, Sitecore Forms The Sitecore Forms application is a powerful tool that enables you to construct web forms on your site without needing developer intervention. It's built into Sitecore and accessible via the Launchpad. Learn More,

How to insert data into sitecore forms programmatically through c# code with custom MVC form ?

Description:I need to insert form data into sitecore experience forms with sitecore 9 and above. I need to create completely custom mvc form and yet insert its data into sitecore forms module.

Posted by: | Posted on: Mar 30, 2023

1 answers

Replies

3

To insert the form’s data into sitecore forms you need to perform following steps
-First we need to create a form in sitecore experience forms editor , lets call it "NewForm" where I have created two fields just to keep it simple.
-Then you can create following methods in the controller to post the form

public ActionResult NewForm()
{
return View();
}
[HttpPost]
public ActionResult NewForm(PersonModel pm)
{
bool status = false;
string message = String.Empty;
try
{
//sitecore fields ids
string NameFieldId = "{B8369FDC-6E08-4054-A89A-FF00D77B1E98}";
string EmailFieldId = "{B7C86CE0-4FC5-47FE-9C9E-9B3D064C6204}";
//sitecore form's id
Guid formId = new Guid("{80E70780-95F7-46A5-8C69-09E89AB5FEF2}");
//List of field ID, Value pair that needs to be passed onto Experience Forms database.
var formFieldList = new List<FormFieldDataModel>();
//Get the submitted form field data in the format that is needed by Experience Forms model
var formNameData = GetNewFormFieldData(NameFieldId, pm.Name);
formFieldList.Add(formNameData);
var formEmailData = GetNewFormFieldData(EmailFieldId, pm.Email);
formFieldList.Add(formEmailData);
_FormsRepository.SaveNewFormData(formId, formFieldList);

status = true;
message = "success";
}
catch (Exception ex)
{
status = false;
message = ex.Message;
}
ViewBag.status = status;
ViewBag.message = message;

return Redirect(Request.UrlReferrer.ToString()+"?status=success");
}
private FormFieldDataModel GetNewFormFieldData(string fieldId, string fieldValue)
{
if (fieldId == null)
return new FormFieldDataModel();
var fieldItem = ItemHelper.GetItem(new Sitecore.Data.ID(fieldId));
if (fieldItem == null) return new FormFieldDataModel();
var formData = new FormFieldDataModel
{
FormFieldName = fieldItem.Name,
FormFieldGuid = fieldItem.ID.ToGuid(),
FormFieldValue = fieldValue,
};
return formData;
}

-ItemHelper Class that is being used in above code

using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Links;
using Sitecore.Web;
using System;
using System.Collections.Generic;
using System.Linq;
public static class ItemHelper
{
public static Database GetCurrentDatabase()
{
if (Sitecore.Context.Database != null)
{
return Sitecore.Context.Database;
}
return Sitecore.Configuration.Factory.GetDatabase("master");
}
public static Item GetItem(Sitecore.Data.ID id)
{
return GetCurrentDatabase().GetItem(id);
}
}

-Then we can create following two Models

public class FormFieldDataModel
{
public string FormFieldValue { get; set; }
public Guid FormFieldGuid { get; set; }
public string FormFieldName { get; set; }
}
public class PersonModel
{
public string Name { get; set; }

public string Email { get; set; }
}

-Now we can create following repository method to save the form to the database

[Service(typeof(INewFormRepo))]
public class NewFormRepo : INewFormRepo
{
private IFormDataProvider _dataProvider;
public NewFormRepo(IFormDataProvider dataProvider)
{
_dataProvider = dataProvider;
}
public void SaveNewFormData(Guid formId, List<FormFieldDataModel> formFieldDataList)
{
Guid sessionId = Guid.NewGuid();
FormEntry formEntry = new FormEntry()
{
Created = DateTime.Now,
FormItemId = formId,
FormEntryId = sessionId,
Fields = new List<FieldData>()
};
foreach (var item in formFieldDataList)
{
AddNewFormFieldData(item, formEntry);
}
_dataProvider.CreateEntry(formEntry);
}

protected void AddNewFormFieldData(FormFieldDataModel postedField, FormEntry formEntry)
{
string str = "System.String";
string fieldValue = postedField.FormFieldValue;
FieldData fieldData = new FieldData()
{
FieldDataId = Guid.NewGuid(),
FieldItemId = postedField.FormFieldGuid,
FormEntryId = formEntry.FormEntryId,
FieldName = postedField.FormFieldName,
Value = fieldValue,
ValueType = str
};
formEntry.Fields.Add(fieldData);
}
}
public interface INewFormRepo
{
void SaveNewFormData(Guid formId, List<FormFieldDataModel> formFieldDataList);
}

-Now we can simply call the controller method from our mvc view as shown.

@using (Html.BeginForm("NewForm", "Forms", FormMethod.Post))
{
<table cellpadding="0" cellspacing="0">
<tr>
<th colspan="2" align="center">Person Details</th>
</tr>
<tr>
<td>Name: </td>
<td>
@Html.TextBoxFor(m => m.Name)
</td>
</tr>
<tr>
<td>Email: </td>
<td>
@Html.TextBoxFor(m => m.Email)
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>

</table>
}
<p>@ViewBag.status @ViewBag.message</p>

Replied by: | Replied on: Apr 18, 2023



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview