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> /// Called bu Umbraco Forms to execute the workflow. /// </summary> /// <param name="record">The record.</param> /// <param name="e">The <see cref="RecordEventArgs"/> instance containing the event data.</param> /// <returns></returns> public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e) { try { if (record == null) { return(WorkflowExecutionStatus.Failed); } if (e == null) { return(WorkflowExecutionStatus.Failed); } // Look for a field with the well-known alias "formEntryID" and insert the unique ids in it var updateRecord = false; foreach (var field in record.RecordFields) { if (field.Value.Alias.ToUpperInvariant() == "FORMENTRYID") { field.Value.Values.Clear(); field.Value.Values.Add(e.Form.Id.ToString()); field.Value.Values.Add(record.UniqueId.ToString()); updateRecord = true; break; } } // Update the record with the id if (updateRecord) { using (RecordStorage recordStorage = new RecordStorage()) { // (note recordStorage.UpdateRecord() doesn't work for the first workflow - see http://issues.umbraco.org/issue/CON-1482) record.RecordData = record.GenerateRecordDataAsJson(); if (record.Id > 0) { record = recordStorage.UpdateRecord(record, e.Form); } else { record = recordStorage.InsertRecord(record, e.Form); } } } } catch (Exception exception) { LogHelper.Error <SaveIdAsFieldWorkflow>(exception.Message, exception); return(WorkflowExecutionStatus.Failed); } return(WorkflowExecutionStatus.Completed); }
/// <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; } }
/// <summary> /// Called bu Umbraco Forms to execute the workflow. /// </summary> /// <param name="record">The record.</param> /// <param name="e">The <see cref="RecordEventArgs"/> instance containing the event data.</param> /// <returns></returns> public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e) { try { if (record == null) { return(WorkflowExecutionStatus.Failed); } if (e == null) { return(WorkflowExecutionStatus.Failed); } List <Exception> settingsErrors = this.ValidateSettings(); if (settingsErrors != null && Enumerable.Any <Exception>(settingsErrors)) { foreach (Exception exception in settingsErrors) { LogHelper.Error <RetentionAfterSetDateWorkflow>(exception.Message, exception); } return(WorkflowExecutionStatus.Failed); } var days = 0; int.TryParse(Days, out days); var weeks = 0; int.TryParse(Weeks, out weeks); var months = 0; int.TryParse(Months, out months); var years = 0; int.TryParse(Years, out years); // Look for a field with the well-known alias "deleteAfter" and insert a retention date in it var updateRecord = false; foreach (var field in record.RecordFields) { if (field.Value.Alias.ToUpperInvariant() == "DELETEAFTER") { field.Value.Values.Clear(); field.Value.Values.Add(DateTime.Today.AddDays(days).AddDays(weeks * 7).AddMonths(months).AddYears(years).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)); updateRecord = true; break; } } // Update the record with the retention date if (updateRecord) { using (RecordStorage recordStorage = new RecordStorage()) { // (note recordStorage.UpdateRecord() doesn't work for the first workflow - see http://issues.umbraco.org/issue/CON-1482) record.RecordData = record.GenerateRecordDataAsJson(); if (record.Id > 0) { record = recordStorage.UpdateRecord(record, e.Form); } else { record = recordStorage.InsertRecord(record, e.Form); } } } } catch (Exception exception) { LogHelper.Error <RetentionAfterSetDateWorkflow>(exception.Message, exception); return(WorkflowExecutionStatus.Failed); } return(WorkflowExecutionStatus.Completed); }