Developer forum

Forum » Development » Manual Insert of Forms for editors

Manual Insert of Forms for editors

Kevin O'Driscoll
Reply

As usual we are doing some custom  contact us forms and this means for various good reasons we have to insert the collected data to the forms for editors structure. So I tried this:

Dynamicweb.Forms.Submit submit = new Dynamicweb.Forms.Submit();
            submit.FormID = 16;
            submit.Date = DateTime.Now;
            submit.Ip = GetUsersIPAddress();
            submit.SessionId = HttpContext.Current.Session.SessionID;
            submit.PageId = Convert.ToInt32(contactUsUser.PageId);
            submit.UserID = 0;
            submit.Referrer = HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];

            Dynamicweb.Forms.Form frm = submit.Form; // get no set??

foreach (var formFields in frm.Fields)
            {
                if (formFields.SystemName == "FormMode")
                {
                    submit.FieldValues.Add(new Dynamicweb.Forms.SubmitData() { FieldID = formFields.ID, Fieldname = formFields.Name, Value = contactUsUser.UserRequestMode });
                }
                if (formFields.SystemName == "Name")
                {
                    submit.FieldValues.Add(new Dynamicweb.Forms.SubmitData() { FieldID = formFields.ID, Fieldname = formFields.Name, Value = contactUsUser.Name });
                } .... adding 23 fields ....

}

submit.Save();

This works since I get a good submit added to the database, but no Field data frm.Fields get inserted to the database. The frm.Fields has a getter but no setter on the property. Is there a secret way to do this?

Rgds Kev


Replies

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Kev

This is how our code does it:

Form f = Form.GetFormById(Converter.ToInt32(Properties.get_Value("FormID"))); //This one must not be overridable

            Submit s = new Submit()
            {
                Ip = Context.Current.Request.UserHostAddress,
                PageId = Pageview.ID,
                SessionId = Context.Current.Session.SessionID,
                Referrer = Context.Current.Request.Form["referer"]
            };

            s.SetForm(f);
            s.Save();

            foreach (Field currentField in f.Fields)
            {
                SubmitData data = new SubmitData()
                {
                    SubmitID = s.ID,
                    FieldID = currentField.ID,
                    Fieldname = currentField.Name
                };

                    data.Value = RequestContext(currentField.SystemName);

                data.Save();

                s.AddFieldValue(data);
            }
Votes for this answer: 1

 

You must be logged in to post in the forum