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; }
/// <summary> /// Gets the field values for the specified field on the specified form. /// </summary> /// <param name="formName">The name of the Contour form.</param> /// <param name="fieldCaption">The caption for the field.</param> /// <returns> /// The field values and labels. /// </returns> public static List<ValueLabelStrings> GetFieldValues(string formName, string fieldCaption) { // Validation. if (string.IsNullOrWhiteSpace(formName) || string.IsNullOrWhiteSpace(fieldCaption)) { return null; } // Get cache of values for specified form/field. var cache = null as InstanceCache<List<ValueLabelStrings>>; lock (FieldValuesLock) { var key = new KeyValueStrings(formName, fieldCaption); if (!FieldValues.TryGetValue(key, out cache)) { cache = new InstanceCache<List<ValueLabelStrings>>(); FieldValues[key] = cache; } } // Get values from cache. return cache.Get(TimeSpan.FromHours(3), () => { var results = new List<ValueLabelStrings>(); using (var formStorage = new FormStorage()) { var form = formStorage.GetForm(formName); using (var recordService = new RecordService(form)) { recordService.Open(); foreach (var field in recordService.Form.AllFields) { if (fieldCaption.InvariantEquals(field.Caption)) { field.PreValueSource.Type.LoadSettings(field.PreValueSource); foreach (var item in field.PreValueSource.Type.GetPreValues(field)) { results.Add(new ValueLabelStrings(item.Id.ToString(), item.Value)); } } } } } return results; }); }
/// <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; } }