Adding records to Umbraco Contour programmatically

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.

btnSubmit_Click
  1. protected void btnSubmit_Click(object sender, EventArgs e)
  2.         {
  3.             var recordStorage = new RecordStorage();
  4.             var formStorage = new FormStorage();
  5.             var form = formStorage.GetForm(new Guid("my form guid"));
  6.  
  7.             var recordService = new RecordService(form);
  8.             recordService.Open();
  9.  
  10.             var record = recordService.Record;
  11.             record.IP = Request.UserHostAddress;
  12.             record.UmbracoPageId = umbraco.presentation.nodeFactory.Node.GetCurrent().Id;
  13.             recordStorage.InsertRecord(record, form);
  14.             foreach (var field in recordService.Form.AllFields)
  15.             {
  16.                 if (field.Caption == "#Email")
  17.                 {
  18.                     var key = Guid.NewGuid();
  19.                     var recordField = new RecordField()
  20.                     {
  21.                         Field = field,
  22.                         Key = key,
  23.                         Record = record.Id,
  24.                         Values = new List<object>() { tbEmail.Text }
  25.                     };
  26.                     var recordFieldStorage = new RecordFieldStorage();
  27.                     recordFieldStorage.InsertRecordField(recordField);
  28.                     record.RecordFields.Add(key, recordField);
  29.                 }
  30.             }
  31.             recordService.Submit();
  32.             recordService.SaveRecord();
  33.             recordService.Dispose();
  34.         }</object>

3 Comments

Harald UlriksenMay 19th, 2010 at 12:58

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.

Kristian RavnevandMay 19th, 2010 at 13:30

Second that Harald :-)

Jason ProtheroMay 21st, 2010 at 22:45

Third that. Thanks for the blog on this.

Leave a comment

Your comment