Surendra Sharma

Surendra Sharma

Search This Blog

Tuesday, February 21, 2017

How to dynamically render WFFM Form in MVC

Yes, you can render WFFM form dynamically instead of assigning through presentation.

Below is a MVC syntax for rendering WFFM form

@Html.Sitecore().Rendering("{F2CCA16D-7524-4E99-8EE0-78FF6394A3B3}", new { Datasource = "<id of the form item>", UniqueId = "<unique id of the form rendering>" })

But wait a minute, what is ID {F2CCA16D-7524-4E99-8EE0-78FF6394A3B3}”?
What to assign at Datasource and UniqueId fields in this syntax?

{F2CCA16D-7524-4E99-8EE0-78FF6394A3B3}" is the item ID of MVC Form and its located at Item Path “sitecore/layout/Renderings/Modules/Web Forms for Marketers/Mvc Form”.

Assign form item id to DataSource and UniqueId fields i.e. replace<id of the form item>” and “<unique id of the form rendering>with WFFM form id that you typically created under “sitecore/system/Modules/Web Forms for Marketers” item.

Example – Put below line in any of your cshtml file in MVC

@Html.Sitecore().Rendering("{F2CCA16D-7524-4E99-8EE0-78FF6394A3B3}", new
                        {
                            Datasource = "{C316B617-0306-454E-8729-E44F49B84CDD}",
                            UniqueId = "{C316B617-0306-454E-8729-E44F49B84CDD}"
                        });

Below is the clearer way to understand it


Sitecore WFFM Dynamic Rendering in MVC
Sitecore WFFM Dynamic Rendering in MVC


I hope you like this Sitecore tip. Stay tuned for more Sitecore related articles.

Till that happy sitecoring :)

Please leave your comments or share this article if it’s useful for you.

Saturday, January 21, 2017

How to avoid “api/sitecore” from Sitecore MVC link URL


There was one Logout link which is working fine in our application, but its URL was looking very weird as http://sitecorelessons/api/sitecore/account/logout.

Client told us to remove that “/api/sitecore” part, even we are also thinking why this “api/sitecore” is coming in URL?

Why “api/sitecore” is coming?

We are generating hyper link with MVC ActionLink() method as 

@Html.ActionLink("Logout", "Logout", "Account", null, new { @class = "btn btn-logout" })

We had used View Rendering for logout section which uses the SitecoreController to render itself and that’s why URL is generated with the default route of Sitecore which ultimately include "api/sitecore" part.

How to solve this?

     1. Create map route for logout action method as

using Sitecore.Pipelines;

public class ConfigureRoutes
{
    public virtual void Process(PipelineArgs args)
    {
        RouteTable.Routes.MapRoute(
            name: "Accountlogout",
            url: "Account/Logout/{id}",
            defaults: new { controller = "Account", action = "Logout", id = UrlParameter.Optional }
            );
    }
}

    2. Point your Controller to move two folder up as

@Html.ActionLink("Logout", "Logout", "../../Account", null, new { @class = "btn btn-logout" })

That’s it. Now when I checked the link on page, it created correctly and working fine as

I hope you like this Sitecore tip. Stay tuned for more Sitecore related articles.

Till that happy sitecoring :)

Please leave your comments or share this article if it’s useful for you.