4

I have query on rendering Sitecore Form.

Can I simply render it by following code on my cshtml ?

@Html.Sitecore().Rendering("/sitecore/layout/Renderings/System/Forms/Mvc Form" , new { DataSource = "/sitecore/Forms/Contact" })

As it is not working for me.

Can anyone help me with this ?

2

There is nothing inherently special about the Experience Forms rendering. It is a rendering like any other Controller Rendering, custom or otherwise. Yes, the Controller itself does call a Sitecore pipeline, but there is nothing stopping you from doing that in your own custom Controller Rendering either.

MVC Form Rendering

If you want to statically bind a rendering in a View, then you can do this like any other statically bound rendering in Sitecore and by also passing in the Datasource parameter to specify the ID or path of the Form item.

@Html.Sitecore().Rendering("{6b5b0fe6-5c85-4487-ba7e-f8c2ecc186a8}", new { DataSource = "{4bedad68-958d-4ea3-aee4-9e6d032eccfa}" })

@Html.Sitecore().Rendering("{6b5b0fe6-5c85-4487-ba7e-f8c2ecc186a8}", new { DataSource = "/sitecore/Forms/Rob Test Form" })

@Html.Sitecore().Rendering("/sitecore/layout/Renderings/System/Forms/Mvc Form", new { DataSource = "/sitecore/Forms/Rob Test Form" })

The GUID above is the Item ID of the MVC Form Controller Rendering item. You can also pass in the path to both the Rendering and the Datasource.

enter image description here

Tested in Sitecore 9.2 and 9.3.

| improve this answer | |
  • This should be the accepted answer. This is what finally worked. – RReilley Sep 23 at 19:08
1

You can use the following method which returns the HTML for the form. The formItemId is the ID of the form datasource ID.

public MvcHtmlString RenderForm(Guid formItemId)
{
    var form = Sitecore.Context.Database.GetItem(ID.Parse(IDs.MvcFormRendering));

    var formRendering = new Rendering
    {
        DataSource = formItemId.ToString(),
        RenderingItemPath = form.Paths.FullPath
    };

    var stringWriter = new StringWriter();
    PipelineService.Get().RunPipeline("mvc.renderRendering", new RenderRenderingArgs(formRendering, stringWriter));
    return new MvcHtmlString(stringWriter.ToString());
}

In the View you can call it with @Html.Raw(RenderForm(formItemId)).

Tested on Sitecore 9.1.

| improve this answer | |
  • Wasn't able to get this to work in 9.3. – RReilley Sep 18 at 19:29
1

One of my colleague Dirk Schäfauer helped me with a way.

Here it is

@Html.Action("Load", "FormBuilder", new RouteValueDictionary() { {"id", "formid.ToString()"}})

Tested and working fine.

Thanks

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.