Hi I need to create Umbraco Forms Entries programmatically but i'm having a litte trouble finding examples, and when I submit to the record storage I come up empty:
public void SaveInfo(MailingListSubmitDto dto)
{
var formStorage = new FormStorage();
var form = formStorage.GetForm(UmbracoFormsConstants.EmailFormName);
var fieldDic = RecordFields(form, dto);
var record = new Record()
{
Created = DateTime.Now,
Form = form.Id,
RecordFields = fieldDic,
State = FormState.Submitted,
RecordData = "[email protected];false"
};
var storage = new RecordStorage(ApplicationContext.Current.DatabaseContext);
storage.InsertRecord(record, form);
}
private static Dictionary<Guid, RecordField> RecordFields(Form form, MailingListSubmitDto dto)
{
var fieldDic = new Dictionary<Guid, RecordField>();
var email = form.AllFields.First(f => f.Alias.ToLower() == UmbracoFormsConstants.EmailFormEmailName.ToLower());
var vip = form.AllFields.First(f => f.Alias.ToLower() == UmbracoFormsConstants.EmailFormVipName.ToLower());
var values = new List<object>() {dto.Email};
email.Values = values;
var emailRecord = new RecordField(email);
emailRecord.Values = email.Values;
var vipRecord = new RecordField(vip);
values = new List<object> {dto.Vip};
vip.Values = values;
vipRecord.Values = vip.Values;
fieldDic.Add(email.Id, emailRecord);
fieldDic.Add(vip.Id, new RecordField(vip));
return fieldDic;
}
Got it working! It's not pretty but other users may be able to read what happens here:
public void SaveInfo(MailingListSubmitDto dto)
{
var formStorage = new FormStorage();
var form = formStorage.GetForm(UmbracoFormsConstants.EmailFormName);
var fieldDic = RecordFields(form, dto);
var record = new Record()
{
Created = DateTime.Now,
Form = form.Id,
RecordFields = fieldDic,
State = FormState.Submitted
};
record.RecordData = record.GenerateRecordDataAsJson();
var storage = new RecordStorage(ApplicationContext.Current.DatabaseContext);
storage.InsertRecord(record, form);
}
private static Dictionary<Guid, RecordField> RecordFields(Form form, MailingListSubmitDto dto)
{
var fieldDic = new Dictionary<Guid, RecordField>();
var email = form.AllFields.First(f => f.Alias.ToLower() == UmbracoFormsConstants.EmailFormEmailName.ToLower());
var vip = form.AllFields.First(f => f.Alias.ToLower() == UmbracoFormsConstants.EmailFormVipName.ToLower());
var emailRecord = new RecordField(email);
emailRecord.Values = new List<object>() { dto.Email };
var vipRecord = new RecordField(vip);
vipRecord.Values = new List<object> { dto.Vip }; ;
fieldDic.Add(email.Id, emailRecord);
fieldDic.Add(vip.Id, vipRecord);
return fieldDic;
}
I have not found the time today to help you out with this, but I will try and ensure our docs get updated soon, so you know how to use the APIs & methods in the codebase.
Create form entry programmatically
Hi I need to create Umbraco Forms Entries programmatically but i'm having a litte trouble finding examples, and when I submit to the record storage I come up empty:
Any ideas?
Hi Kenneth,
What errors do you get, if any?
What results are you seeing, is this persisted at all in the DB, if you look through the DB table/s associated with Forms they start with UF
Thanks,
Warren
Yes the email is actually stored, But my view is this: The "Vip" boolean is not stored though
It seems that my recorddata is not wellformed but I cant seem to figure out how it should be...
Got one step closer:
record.RecordData = record.GenerateRecordDataAsJson();
this generates the nescessary json for the form data, but the Vip (bool) is still not persisted in the database.
Got it working! It's not pretty but other users may be able to read what happens here:
I am glad you stuck through with this Kenneth.
I have not found the time today to help you out with this, but I will try and ensure our docs get updated soon, so you know how to use the APIs & methods in the codebase.
is working on a reply...