Copied to clipboard

Flag this post as spam?

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


  • Kristian Ravnevand 94 posts 214 karma points
    May 19, 2010 @ 09:28
    Kristian Ravnevand
    2

    Adding records to Contour from usercontrol

    Hi, how can I add a record to contour from codebehind / a usercontrol.

    This is my code so far:

                //The form
                RecordStorage recordStorage = new RecordStorage();
                FormStorage formStorage = new FormStorage();
                Form form = formStorage.GetForm(new Guid("d9b5526e-38e5-48e5-a284-166bf3f5b4b0"));

                //Recordservice
                RecordService recordService = new RecordService(form);
                recordService.Open();

                //Record
                Record record = recordService.Record;

                //Store
                record.UmbracoPageId = current.Id;
                recordStorage.InsertRecord(record, form);

                recordService.Submit();
                recordService.SaveRecord();
                recordService.Dispose();

    This creates a record just fine, but I can't figure out how to add field values. I thought something like this should do it, but it don't:

                foreach (Field field in form.AllFields)
                {
                    if (field.Caption == "From")
                    {
                        RecordField rf = new RecordField();
                        rf.Values = new List<object>();
                        rf.Values.Add("from_value");

                        record.RecordFields.Add(Guid.NewGuid(), rf);
                    }
                }

    The rf.Values.Add results in an Object rreference not set...

    Anybody know a solution to this?

  • Kristian Ravnevand 94 posts 214 karma points
    May 19, 2010 @ 09:30
    Kristian Ravnevand
    0

    Sorry forgot a line in the foreach statement:

                        RecordField rf = new RecordField();
                        rf.Field = field;

  • Rasmus Lynggaard 118 posts 325 karma points
    May 19, 2010 @ 13:33
    Rasmus Lynggaard
    2

    Just blogged about it: http://bit.ly/c5M5UU

  • Kristian Ravnevand 94 posts 214 karma points
    May 19, 2010 @ 14:15
    Kristian Ravnevand
    0

    Fabolus thank you so much :-D

  • Jason Prothero 422 posts 1243 karma points c-trib
    May 21, 2010 @ 23:44
    Jason Prothero
    0

    Thanks!

  • Rick 92 posts 278 karma points
    Sep 11, 2012 @ 11:09
    Rick
    0

    Hi guys,

    I know there is a comment above that has been marked as the topic solution; however, the link to the posted blog is dead!

    Would anyone kind enough out there, please provide me with a link to the blog which achieves adding records from a user control - or perhaps some snippets of code that helps me!

    My code so far is similar to the above - but I've posted it anyway:

        protected void btnSubmit_Click(object sender, EventArgs e)

        {

            //Get currentNodeId

            dynamic currentNode = new DynamicNode(Node.GetCurrent());

            //The form

            RecordStorage recordStorage = new RecordStorage();

            FormStorage formStorage = new FormStorage();

            Form form = formStorage.GetForm(new Guid("1db4b81e-ec1e-45d6-a44e-a89690229d22"));

            //Recordservice

            RecordService recordService = new RecordService(form);

            recordService.Open();

            //Record

            Record record = recordService.Record;

            //Store

            record.UmbracoPageId = currentNode.Id;

            recordStorage.InsertRecord(record, recordService.Form);

            recordService.Submit();

            recordService.SaveFormToRecord();

            recordService.Dispose();

     

    foreach (RecordField rf in record.RecordFields.Values)

            {

    // Check form has the field"Firstname"

                if (rf.Field.Caption == "Firstname")

                {

                    var firstName = record.GetRecordField("Firstname");

                    firstName.Values.Clear();

    // Add the inserted text from the textbox to the value of "Firstname"

                    firstName.Values.Add(txtFirstName.Text);

                }

    // Check the form has the field "Surname"

                if (rf.Field.Caption == "Surname")

                {

    // Add the inserted text from the textbox to the value of "Surname"

                    var surname = record.GetRecordField("Surname");

                    surname.Values.Clear();

                    surname.Values.Add(txtSurname.Text);

                }

            }

    }

    Similar to the OP, when btnSubmit is clicked, a record is inserted into Umbraco Contour - however, the values that are inserted above (Firstname and Surname) are not! Please see screenshot attached:

    The GUID in my code matches that of the form that is set up - hence why a record is inserted.

    Thanks in advance!

  • Rasmus Lynggaard 118 posts 325 karma points
    Sep 12, 2012 @ 12:16
    Rasmus Lynggaard
    1

    Since my blog is no longer online, I'll just put the post in here.

     

    2010-05-19 12:18:03

    I just had to programmatically add records to an Umbraco Contour Form. Since I couldn't find any documentation on how to do this, I thought I would post how I did this.

    I have an input box (tbEmail) and a button (btnSubmit). On the click event of btnSubmit I got the following code, that adds a record to the form.

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
    //The Form
    var recordStorage = new RecordStorage();
    var formStorage = new FormStorage();
    var form = formStorage.GetForm(new Guid("my form guid"));

    //Recordservice
    var recordService = new RecordService(form);
    recordService.Open();

    var record = recordService.Record;
    record.IP = Request.UserHostAddress;
    record.UmbracoPageId = umbraco.presentation.nodeFactory.Node.GetCurrent().Id;
    recordStorage.InsertRecord(record, form);
    foreach (var field in recordService.Form.AllFields)
    {
    if (field.Caption == "#Email")
    {
    var key = Guid.NewGuid();
    var recordField = new RecordField()
    {
    Field = field,
    Key = key,
    Record = record.Id,
    Values = new List<object>() { tbEmail.Text }
    };
    var recordFieldStorage = new RecordFieldStorage();
    recordFieldStorage.InsertRecordField(recordField);
    record.RecordFields.Add(key, recordField);
    }
    }
    recordService.Submit();
    recordService.SaveRecord();
    recordService.Dispose();
    }

    There was also a comment that makes good sence:

    Nice post. I guess this should be in the API documentation. There's a possible issue with not creating all the fields to begin with, as it will be a source of possible problems if you open the form later on and try to access a field which does not exists. Question is if it's better to create all the fields and leave values as a new empty List.

  • Rick 92 posts 278 karma points
    Sep 12, 2012 @ 12:43
    Rick
    0

    Thank you very much - this works brilliantly!

  • Matthias Bier 30 posts 90 karma points
    Nov 28, 2012 @ 10:24
    Matthias Bier
    0

    Hi,

    I'm creating records with this method with the /base service. It seems to work but there's a little bug somewhere. After the record is submitted, the contour page shows all values as empty.
    Then after I do something with the record (like approve, edit) suddenly the values are shown.

    I checked in the database and saw that the record in the table UFRecordsXml is missing all form field entries. So in the table there's only the header (first two entries):


    After I approve or edit the record in contour, the fields are added to the XML. I guess I'm doing something wrong in the code... My code looks like this:

    RecordStorage rs  = new RecordStorage();
    FormStorage fs = new FormStorage();
    Form form = fs.GetForm(new Guid("707c5d27-bb7c-4de1-8b25-b7e771e730a8"));
    RecordService recServ = new RecordService(form);
    recServ.Open();
     
    Record rec = recServ.Record;            
    rec.IP = "192.168.230.111";
    rec.UmbracoPageId = 1243;
    rs.InsertRecord(rec, form);
     
    foreach (var item in recServ.Form.AllFields)
    {
    Guid key = new Guid();
    RecordField rf = new RecordField();
    rf.Field = item;
    rf.Key = key;
    rf.Record = rec.Id;
    List
    rf.Values = vl;
    switch (item.Caption)
    {
       case "Name / Company":
    rf.Values = new List
    break;
       case "Email":
    rf.Values = new List
    break;
       case "Message":
    rf.Values = new List
    break;
       case "Topic":
    rf.Values = new List
    break;
       case "RequestIP":
    rf.Values = new List
    break;
       default:
    break;
    }
    RecordFieldStorage recFieldStorage = new RecordFieldStorage();
    recFieldStorage.InsertRecordField(rf);
    recFieldStorage.Dispose();      
    }            
    recServ.Submit();            
    recServ.SaveFormToRecord();
    recServ.Dispose();
    rs.Dispose();
    fs.Dispose();

     

    Thank you

    Matthias 

    Updated:
    Sorry, stupid mistake: I assigned an empty Guid to the fields, that's why trying to add the fields with rec.RecordFields.Add(key, rf); alwasy failed..

    Now it works fine. 

Please Sign in or register to post replies

Write your reply to:

Draft