Copied to clipboard

Flag this post as spam?

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


  • Si Burgess 15 posts 56 karma points
    Apr 18, 2016 @ 16:17
    Si Burgess
    0

    How to provide a separate database connection to Umbraco Forms?

    Hi,

    I have Umbraco Forms 4.2.1 installed in 7.3.8 and running ok.

    We'd like to have the Forms tables in a separate database so that user submitted data is separate from the site database.

    I've had a stab at setting

    Umbraco.Forms.Core.Configuration.DatabaseConnection = "my other db string";
    

    in OnApplicationStarting but the main Umbraco connection still gets used.

    Any ideas how I can override the database connection Forms uses?

    Thanks, Si

  • Domenic 42 posts 128 karma points
    Jul 28, 2016 @ 15:52
    Domenic
    0

    Hello, I know this is an old post... but I have read elsewhere that Umbraco Forms stores data as a set of files on server rather than a db, and that data cannot be transferred to another db.

    Dom

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Jul 28, 2016 @ 16:11
    Nicholas Westby
    0

    Dom,

    I believe the OP was referring to data submitted to forms, rather than the form definitions themselves. I believe you are correct that form definitions are stored on the file system, but I believe data submitted by users is stored in the database (with the exception of uploaded files, which I believe are indeed stored on the file system).

    Si,

    You could probably accomplish what you are trying to do by creating a custom workflow step that stores data to a different database. Of course, this means that data will be stored in the current database as well. Also, it means that you'd see only the data in the current database when viewing form submissions from the Umbraco Forms UI. So, you'd have to build some other UI to view the data in the other database.

    I am not sure if storing Umbraco Forms data exclusively in another database is a supported scenario.

  • Domenic 42 posts 128 karma points
    Jul 29, 2016 @ 13:46
    Domenic
    0

    Thanks Nicholas!

  • Si Burgess 15 posts 56 karma points
    Jul 28, 2016 @ 16:17
    Si Burgess
    0

    Thanks for the replies guys.

    Nicolas - your right, it was storing the submitted data that I want to store in a separate db.

    The client had to live with the data staying in the Umbraco db as I found there was no way to specify/intercept the connection string Umbraco Forms uses.

  • Andrew Vennells 16 posts 101 karma points
    Nov 21, 2016 @ 23:22
    Andrew Vennells
    0

    I have done something similar to this. The client wanted more data than was being stored by Umbraco forms and also wanted a central view for both Umbraco forms and some custom forms on their sites.

    I solved this by first creating a custom forms controller, inheriting the nativ forms controller. You can then override the OnFormHandled method to get the data from UF and store it in the custom tables. You'll then need to update the Forms.cshtml BeginForm statement to use your custom forms controller. Then using EF code first to create my tables. This could be in the same db you have Umbraco on or a different db based on the Entities connection string.

    Here is my custom forms controller and the OnFormHandled override I used to store the forms data:

    public class CustomUmbracoFormsController : UmbracoFormsController
        {
            /// <summary>
            /// Called when [form handled].
            /// </summary> 
            /// <param name="form">The form.</param>
            /// <param name="model">The model.</param>
            protected override void OnFormHandled(Form form, FormViewModel model)
            {
                base.OnFormHandled(form, model);
    
                if (!ModelState.IsValid) return;
    
                var formFields = form.AllFields.ToList();
                var recordId = Guid.NewGuid();
                var url = HttpContext.Request.UrlReferrer != null ? HttpContext.Request.UrlReferrer.ToString() : string.Empty;
    
                using (var db = new Entities())
                {
                    try
                    {
                        db.UmbracoFormRecords.Add(new UmbracoFormRecord
                        {
                            RecordId = recordId,
                            ReferrerUrl = url,
                            FormId = form.Id,
                            Timestamp = DateTime.Now
                        });
    
                        foreach (var formField in formFields)
                        {
                            object[] fieldValue;
                            var hasValue = model.FormState.TryGetValue(formField.Id.ToString(), out fieldValue);
    
                            db.UmbracoFormInput.Add(new UmbracoFormInput
                            {
                                InputId = Guid.NewGuid(),
                                RecordId = recordId,
                                FieldTypeKey = formField.FieldTypeId,
                                FieldTypeName = formField.FieldType.Name,
                                FieldName = formField.Caption,
                                FieldId = formField.Id,
                                FieldValue = hasValue && fieldValue.Length > 0 ? fieldValue[0].ToString() : string.Empty
                            });   
                        }
    
                        var workflowController = new WorkflowController();
                        var workflows = workflowController.GetAllWorkflows(form.Id);
    
                        foreach (var workflow in workflows)
                        {
                            db.AddWorkflows(workflow, recordId);
                        }
    
                        db.SaveChanges();
                    }
                    catch (DbEntityValidationException e)
                    {
                        Log.Add(LogTypes.Save, CurrentPage.Id, "Error saving umbraco forms data");
                        LogHelper.Debug(e.GetType(), e.EntityValidationErrors.ToString());
                    }
                } 
            }
        }
    

    I also created an Anglular plugin and extended the Backoffice to view the data. Let me know if you need more info

Please Sign in or register to post replies

Write your reply to:

Draft