These days I got a few customers who all needed to get a form module installed in their Sitecore installation. Therefore there might be a few blog post on the Official Sitecore forms module, Webforms for Marketers.

This time I wanted to improve the insert form button in the ribbon. To insert a form on a page, you need to find the item you want it on, go to the Presentation ribbon, and choose "Insert form". This is all good, but when you have already inserted a form to a page, the button still does an insert new form, and you can’t edit it from the ribbon menu.

In my previous blog post,"Adding an edit button to the Web Forms for Marketers module", I did an edit button in the ribbon, this time I wanted to improve the insert button to only show, if there isn’t already a form inserted. To do this we need to override the buttons command, with a new one that derives from the old one and override the method QueryState. We implement the following:

        public override Sitecore.Shell.Framework.Commands.CommandState QueryState(Sitecore.Shell.Framework.Commands.CommandContext context)
        {
            if (context.Items.Length > 0 && !context.Items[0].Security.CanWrite(Context.User))
            {
                return CommandState.Disabled;
            }
            if (!String.IsNullOrEmpty(FormUtil.GetFormId(context.Items[0])))
            {
                return CommandState.Hidden;
            }
            return base.QueryState(context);
        }

The method from our formUtil, GetformId, can be found in my previous blog post.

Patching is done by replacing the existing insert:form command like this:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <commands>
            <command patch:instead="*[@name='forms:insert']" name="forms:insert" type="CustomizeSitecore.Forms.Commands.InsertForm, CustomizeSitecore.Forms"/>
        </commands>
    </sitecore>
</configuration>

That’s it, a simple solution to improve this button. Combined with the edit button from the previous blog post, this makes the ribbon the preferred entrance to the forms module. All it needs is just a command that will enable removing an existing form from a page. More on this in a future blog post.