/// <summary> /// Stores a form record. /// </summary> /// <param name="form">The form to store a record to.</param> /// <param name="fieldValues">The field values to store.</param> /// <param name="ipAddress">The user's IP address.</param> /// <param name="pageId">The current page ID.</param> /// <param name="recordId">Outputs the ID of the record stored to Contour.</param> /// <returns>True, if the record was stored; otherwise, false.</returns> private static bool StoreRecord(Form form, Dictionary<string, List<object>> fieldValues, string ipAddress, int pageId, out Guid recordId) { using (var recordStorage = new RecordStorage()) using (var recordService = new RecordService(form)) { // Open record service. recordService.Open(); // Create record. var record = recordService.Record; record.IP = ipAddress; record.UmbracoPageId = pageId; record = recordStorage.InsertRecord(record, form); // Assign field values for record. foreach (var field in recordService.Form.AllFields) { // Check which field this is. var knownField = false; var values = default(List<object>); if (fieldValues.TryGetValue((field.Caption ?? string.Empty).ToLower(), out values)) { knownField = true; } // If the field is one of those that are known, store it. if (knownField && values != null && values.Any(x => x != null)) { // Create field. var key = Guid.NewGuid(); var recordField = new RecordField { Field = field, Key = key, Record = record.Id, Values = values }; // Store field. using (var recordFieldStorage = new RecordFieldStorage()) { recordFieldStorage.InsertRecordField(recordField); } // Add field to record. record.RecordFields.Add(key, recordField); } } // Submit / save record. recordService.Submit(); var result = recordService.SaveFormToRecord(); recordId = record.Id; return result; } }
public static string SaveToContour(this DynaForm dynaform, string formGuid, out string insertedRecordId, string userIpAddress = "", int umbracoPageId = 0) { var message = ""; insertedRecordId = ""; using (var recordStorage = new RecordStorage()) using (var formStorage = new FormStorage()) { var form = formStorage.GetForm(new Guid(formGuid)); using (var recordService = new RecordService(form)) { recordService.Open(); var record = recordService.Record; record.IP = userIpAddress; record.UmbracoPageId = umbracoPageId; recordStorage.InsertRecord(record, form); foreach (var field in recordService.Form.AllFields) { string currentFieldValue = ""; string contourFieldName = field.Caption.TrimStart('#'); if (dynaform.ModelDictionary.ContainsKey("f" + field.Id.ToString().Replace('-', '_'))) { currentFieldValue = dynaform.ModelDictionary["f" + field.Id.ToString().Replace('-', '_')].ToString(); } else if (dynaform.ModelDictionary.ContainsKey(contourFieldName)) { currentFieldValue = dynaform.ModelDictionary[contourFieldName].ToString(); } var key = Guid.NewGuid(); var recordField = new RecordField { Field = field, Key = key, Record = record.Id, Values = new List<object> { currentFieldValue } }; using (var recordFieldStorage = new RecordFieldStorage()) recordFieldStorage.InsertRecordField(recordField); record.RecordFields.Add(key, recordField); insertedRecordId = record.Id.ToString(); } recordService.Submit(); recordService.SaveFormToRecord(); } message=form.MessageOnSubmit; } return message; }