Web Forms for Marketers - Submit form data program

2019-04-15 02:52发布

问题:

I'm currently scoping whether or not to include Web Forms for Marketers on a project I'm currently working on. The web site will be a responsive design with a rich UI and so I would like to have full control over the rendering of the form, e.g. labels, input fields etc and would rather not have to modify the WFFM SitecoreSimpleFormAscx.ascx file or the associated css file.

Is there currently any way, using C# code, to submit some form data to a Web Forms for Marketers form that is already created in Sitecore (assuming I have knowledge of all the fields)? The WFFM reference guide has a code snippet for accessing already submitted web form data but nothing for just submitting some.

回答1:

If you don't want to dig into custom controls for the forms you might try performing a POST to a page that contains the WFFM form via JavaScript from your responsive page. Alternatively you could embed and hide the wffm form on your page and stunt drive it with script again. I think the second approach would be better as it would allow you to more directly respond to validation failures.



回答2:

You could submit form code to the WFFM DB using the following code, and it would show up seamlessly in the report page of that form:

Say you have your list of fields populated in a list of this class:

public class WffmField
    {
        public string FieldName { get; set; }
        public string FieldGuid { get; set; }
        public string FieldValue { get; set; }
    }

the field guid would be the guid from sitecore:

You can then save to the WFFM database:

// This should be populated with the data you want to send to the WFFM database
var fields = new List<WffmField>(); 
var wffmDatabaseFields = fields.Select(GetWFFMDatabaseField).ToList();

Sitecore.Forms.Data.DataManager.InsertForm(
    formId: new Sitecore.Data.ID("<Form guid here>"),
    fields: new AdaptedResultList(wffmDatabaseFields),
    sessionID: AnalyticsTracker.SessionId,
    data: null);

Hope this helps!