asp.net-mvckenticokentico-mvckentico-12

How to use BizForms in Kentico 12 without page builder


Kentico 12 only supports forms using the "Form" page-builder widget out of the box.

Can anyone provide examples of how one might utilise BizForms on an MVC _Layout.cshtml or in pages that do not use the page builder?

Acceptance criteria:


Solution

  • The OP posted a blog about how to make this work.

    The solution requires using some of Kentico's internal APIs for Form Builder rendering with Form Widgets and putting that code into a Controller action.

    var formInfo = BizFormInfoProvider
        .GetBizFormInfo(formName, SiteContext.CurrentSiteName);
    
    string className = DataClassInfoProvider
        .GetClassName(formInfo.FormClassID);
    
    var existingBizFormItem = className is null
        ? null
        : BizFormItemProvider
            .GetItems(className)?.GetExistingItemForContact(
               formInfo, contactContext.ContactGuid);
    
    var formComponents = formProvider
        .GetFormComponents(formInfo)
        .GetDisplayedComponents(
          ContactManagementContext.CurrentContact, 
          formInfo, existingBizFormItem, visibilityEvaluator);
    
    var settings = new JsonSerializerSettings
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver(),
        TypeNameHandling = TypeNameHandling.Auto,
        StringEscapeHandling = StringEscapeHandling.EscapeHtml
    };
    
    var formConfiguration = JsonConvert.DeserializeObject<FormBuilderConfiguration>(
        formInfo.FormBuilderLayout, settings);
    
    return new FormWidgetViewModel
    {
        DisplayValidationErrors = true,
        FormComponents = formComponents.ToList(),
        FormConfiguration = formConfiguration,
        FormName = formName,
        FormPrefix = Guid.NewGuid().ToString(),
        IsFormSubmittable = true,
        SiteForms = new List<SelectListItem>(),
        SubmitButtonImage = formInfo.FormSubmitButtonImage,
        SubmitButtonText = string.IsNullOrEmpty(formInfo.FormSubmitButtonText) 
          ? ResHelper.GetString("general.submit")
          : ResHelper.LocalizeString(formInfo.FormSubmitButtonText)
    };
    

    I took that idea and wrote a follow up post, Kentico EMS: MVC Widget Experiments Part 3 - Rendering Form Builder Forms Without Widgets, which shows we can also use Kentico's pre-built Form Widget view code to also get the expected rendering and form submission functionality.

    <!-- ~/Views/Form/Form.cshtml -->
    
    @using Kentico.Forms.Web.Mvc;
    @using Kentico.Forms.Web.Mvc.Widgets;
    @using Kentico.Forms.Web.Mvc.Widgets.Internal
    
    @model FormWidgetViewModel
    
    @{
        var config = FormWidgetRenderingConfiguration.Default;
    
        // @Html.Kentico().FormSubmitButton(Model) requires 
        // this ViewData value to be populated. Normally it
        // executes as part of the Widget rendering, but since
        // we aren't rendering a Widget, we have to do it manually
    
        ViewData.AddFormWidgetRenderingConfiguration(config);
    }
    
    @using (Html.Kentico().BeginForm(Model))
    {
        @Html.Kentico().FormFields(Model)
    
        @Html.Kentico().FormSubmitButton(Model)
    }