Last active
April 28, 2021 19:30
-
-
Save hfloyd/7269c941244673757e115b84f84c3a5e to your computer and use it in GitHub Desktop.
Umbraco 7: Example Code for 'Displaying Results After an Umbraco Form Submission'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@inherits UmbracoViewPage | |
@{ | |
var sFormGuid = "26fcda6d-6a2f-46be-a164-a5dbb235fcd8"; //CG Demo form GUID | |
var formData = new Dragonfly.Forms.Models.FormWithRecords(sFormGuid); //Using FormWithRecords() Model | |
var surveyModel = new FormCgDemo(sFormGuid); //Using Custom data model (defined in an external file) | |
} | |
<!-- Examples of using the models --> | |
<p>Number of responses: @formData.RecordsAll.Count</p> | |
<p>Number of responses: @surveyModel.Records.Count()</p> | |
@{ | |
var yearsData = surveyModel.Records.Select(n => n.HowManyTimesBeenToCodegarden).ToList(); | |
} | |
<p>Average years attending: @yearsData.Average()</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SEE: https://github.com/hfloyd/Dragonfly.UmbracoFormsFields for referenced 'FormHelper" code | |
public class FormCgDemo | |
{ | |
public FormWithRecords FormWithRecords { get; internal set; } | |
public IEnumerable<FormCgDemoRecord> Records { get; internal set; } | |
public FormCgDemo(string FormGuid) | |
{ | |
this.FormWithRecords = new FormWithRecords(FormGuid); | |
var formRecords = new List<FormCgDemoRecord>(); | |
foreach (var record in FormWithRecords.RecordsAll) | |
{ | |
var formGuid = new Guid(FormGuid); | |
var typedRecord = new FormCgDemoRecord(); | |
//Standard Record Info | |
typedRecord.RecordId = record.Id; | |
typedRecord.State = record.State; | |
typedRecord.RecordUniqueId = record.UniqueId; | |
typedRecord.Created = record.Created; | |
typedRecord.IP = record.IP; | |
typedRecord.MemberKey = record.MemberKey; | |
typedRecord.UmbracoPageId = record.UmbracoPageId; | |
typedRecord.Updated = record.Updated; | |
//Custom Field Values | |
typedRecord.HowManyTimesBeenToCodegarden = FormHelper.GetIntFieldValue(record, "HowManyTimesBeenToCodegarden"); | |
typedRecord.FavoriteEveningBeverage = FormHelper.GetStringFieldValue(record, "FavoriteEveningBeverage"); | |
typedRecord.OtherBeverage = FormHelper.GetStringFieldValue(record,"OtherBeverage"); | |
typedRecord.HowMuchLikeBeer = FormHelper.GetIntFieldValue(record, "HowMuchLikeBeer"); | |
typedRecord.RecommendAttendingCodegardenRaw = FormHelper.GetStringFieldValue(record, "RecommendAttendingCodegarden"); | |
typedRecord.FirstName = FormHelper.GetStringFieldValue(record, "FirstName"); | |
typedRecord.LastName = FormHelper.GetStringFieldValue(record, "LastName"); | |
typedRecord.CompanyName = FormHelper.GetStringFieldValue(record, "CompanyName"); | |
typedRecord.EmailAddress = FormHelper.GetStringFieldValue(record, "EmailAddress"); | |
formRecords.Add(typedRecord); | |
} | |
this.Records = formRecords; | |
} | |
} | |
public class FormCgDemoRecord | |
{ | |
public string IP { get; internal set; } | |
public Guid RecordUniqueId { get; internal set; } | |
public int RecordId { get; internal set; } | |
public Umbraco.Forms.Core.Enums.FormState? State { get; internal set; } | |
public object MemberKey { get; internal set; } | |
public int UmbracoPageId { get; internal set; } | |
public DateTime Created { get; internal set; } | |
public DateTime Updated { get; internal set; } | |
public int HowManyTimesBeenToCodegarden { get; internal set; } | |
public string FavoriteEveningBeverage { get; internal set; } | |
public string OtherBeverage { get; internal set; } | |
public int HowMuchLikeBeer { get; internal set; } | |
public string RecommendAttendingCodegardenRaw { get; internal set; } | |
public string FirstName { get; internal set; } | |
public string LastName { get; internal set; } | |
public string CompanyName { get; internal set; } | |
public string EmailAddress { get; internal set; } | |
public FormCgDemoRecord() { } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Dragonfly.Forms.Models | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Umbraco.Forms.Core; | |
using Umbraco.Forms.Core.Enums; | |
using Umbraco.Forms.Data.Storage; | |
public class FormWithRecords | |
{ | |
public Guid FormGuid { get; } | |
public Umbraco.Forms.Core.Form Form { get; } | |
public IEnumerable<string> FormInfo { get; } | |
public IEnumerable<Umbraco.Forms.Core.Field> Fields { get; } | |
public List<Record> RecordsAll { get; } | |
public List<Record> RecordsApproved { get;} | |
public FormWithRecords(string FormGuidString) | |
{ | |
//Set basic properties | |
this.FormGuid = new Guid(FormGuidString); | |
//The "Library" functions cause "There is already an open DataReader associated with this Command which must be closed first." errors | |
//see: https://our.umbraco.org/forum/umbraco-forms/78207-working-with-record-data-there-is-already-an-open-datareader-associated | |
//this.RecordsApproved = Library.GetApprovedRecordsFromForm(FormGuidString).Items; | |
//this.RecordsAll = Library.GetRecordsFromForm(FormGuidString).Items; | |
//Alternative: | |
using (var formStorage = new FormStorage()) | |
{ | |
using (var recordStorage = new RecordStorage()) | |
{ | |
var form = formStorage.GetForm(this.FormGuid); | |
var allRecords = recordStorage.GetAllRecords(form).ToList(); | |
this.RecordsAll = allRecords; | |
this.RecordsApproved = allRecords.Where(x => x.State == FormState.Approved).ToList(); | |
} | |
} | |
// Get form info | |
using (FormStorage formStorage = new FormStorage()) | |
{ | |
this.Form = formStorage.GetForm(this.FormGuid); | |
} | |
//Get all fields | |
var fields = new List<Field>(); | |
var exampleRecord = RecordsAll.First(); | |
foreach (var fieldItem in exampleRecord.RecordFields) | |
{ | |
var recField = fieldItem.Value; | |
var field = recField.Field; | |
fields.Add(field); | |
} | |
this.Fields = fields; | |
} | |
public IEnumerable<KeyValuePair<Guid, RecordField>> AllFieldData(string FieldAlias, bool ApprovedOnly = true) | |
{ | |
var returnData = new List<KeyValuePair<Guid, RecordField>>(); | |
var records = ApprovedOnly ? this.RecordsApproved : this.RecordsAll; | |
foreach (var record in records) | |
{ | |
var match = record.RecordFields.Where(n => n.Value.Alias == FieldAlias).First(); | |
returnData.Add(match); | |
} | |
return returnData; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See the article "Displaying Results After an Umbraco Form Submission" for an explanation of usage.