In a certain scenario I need to programmatically change the WFFM Form presented in the Form Renderer. The Form Renderer itself is added to a Placeholder via the Presentation Details.
I am able to access the Form Render and set the FormID and update its parameters to the ID of the Form I want to display instead. However, its as if Sitecore isn't honouring these properties and displaying the original form even when I move my code earlier in the page life cycle e.g. Page_Init. My code is as follows
// Pass in controls of the sublayout
public FormRender SetFormRender(Control control, string dataSource)
{
FormRender formRender = null;
// loop through controls contained in passed in control
foreach (Control child in control.Controls)
{
if (child is FormRender)
{
formRender = child as FormRender;
if (formRender != null)
{
formRender.FormID = dataSource;
formRender.Parameters = "FormID=" + HttpUtility.HtmlEncode(dataSource);
break;
}
}
// If control has controls pass to this method for recursion
if (control.HasControls())
{
var nestedFormRender = SetFormRender(child, dataSource);
if (nestedFormRender != null && formRender == null)
{
formRender = nestedFormRender;
break;
}
}
}
return formRender;
}
I found a resolution based on Mike Reynolds blog as Ahmed Okour suggested got me 50% of the way there but required additional work.
I created a new FormRender that exposed a public property for the overriding Form Id to be passed. This new FormRender implements the existing one however overriding the base OnInit() still produces a protected method meaning it can't be called when we need it. Therefore I created a non-overriding public OnInit() method:
I duplicated the FormRender in Sitecore at the path
/sitecore/layout/Renderings/Modules/Web Forms for Marketers/
, referencing thePublicFormRender
and used that to add Forms in the Presentation Details