Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kenneth Jakobsen 67 posts 203 karma points hq
    Apr 25, 2016 @ 12:53
    Kenneth Jakobsen
    0

    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:

    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;
        }
    

    Any ideas?

  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Apr 25, 2016 @ 13:01
    Warren Buckley
    0

    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

  • Kenneth Jakobsen 67 posts 203 karma points hq
    Apr 25, 2016 @ 13:07
    Kenneth Jakobsen
    0

    Yes the email is actually stored, But my view is this: The "Vip" boolean is not stored though

    enter image description here

  • Kenneth Jakobsen 67 posts 203 karma points hq
    Apr 25, 2016 @ 13:16
    Kenneth Jakobsen
    0

    It seems that my recorddata is not wellformed but I cant seem to figure out how it should be...

  • Kenneth Jakobsen 67 posts 203 karma points hq
    Apr 25, 2016 @ 13:22
    Kenneth Jakobsen
    0

    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.

  • Kenneth Jakobsen 67 posts 203 karma points hq
    Apr 25, 2016 @ 13:35
    Kenneth Jakobsen
    101

    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;
        }
    
  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Apr 25, 2016 @ 14:16
    Warren Buckley
    0

    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.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies