Linus Ekström
Oct 30, 2008
  4332
(2 votes)

Inserting XForms as dynamic content

With the introduction of dynamic content in EPiServer CMS 5 comes a lot of new posibilities for the editors to make the page more dynamic. One sample that we shipped with the R2 release was the page property plugin. This makes it possible to insert a property from another page in a xhtml editor of a page. This can for instance be used to insert a simple value type such as a copyright text that is just a string property on another page.

As we use the Property web control to present the content of the property it's possible to insert more complex objects with the page property adapter as it just loads the PropertyControl associated with the PropertyData object. One great example is the XForms property. This makes it possible to insert a form on whatever page you want that has at least one xhtml string with dynamic content enabled. For this to work as expected we had to make some changes to the public templates as the form previously often had dependancies to the actual page/user control that was used to present the form. The XFormControl now has a static event, ControlSetup, that makes it possible to add a global behaviour to the XFormControl wherever it may be instantiated. Here is the actual code that attaches event to forms that is located in the global.asax.cs file in the R2 version of the public templates:

protected void Application_Start(Object sender, EventArgs e)
        {
            XFormControl.ControlSetup += new EventHandler(XForm_ControlSetup);
        }

public void XForm_ControlSetup(object sender, EventArgs e)
        {
            XFormControl control = (XFormControl)sender;

            control.BeforeLoadingForm += new LoadFormEventHandler(XForm_BeforeLoadingForm);
            control.ControlsCreated += new EventHandler(XForm_ControlsCreated);
            control.BeforeSubmitPostedData += new SaveFormDataEventHandler(XForm_BeforeSubmitPostedData);
            control.AfterSubmitPostedData += new SaveFormDataEventHandler(XForm_AfterSubmitPostedData);
        }

This makes it possible to still have your own custom logic when a form is loaded or posted even though you have no idea of where the form might appear. If you start to develop with the public templates in the R2 release you don't have to do anything to get this working but you should be aware that you will have logic to the form events in your global.asax.cs file. If you have an old project and upgrade it to R2 you will have to move your xform event handlers from your xform template page(s) to some global place, like the global.asax.cs file (or at least to attach your event handlers globally). You still have the option to attach local event handlers for specific form pages, just be aware that you might have global events that affects the form.

Oct 30, 2008

Comments

Oct 12, 2010 10:33 AM

Thank you for your article. I need to add a mailto everytime the editor adds a text field that validates to an email address. Do you have any clues how to do that? Thanks.
/ Victor Dollero (victor.dollero@fortunecookie.co.uk)

Please login to comment.
Latest blogs
Multiple Anonymous Carts created from external Head front fetching custom Api

Scenario and Problem Working in a custom headless architecture where a NextJs application hosted in Vercel consumes a custom API built in a...

David Ortiz | Oct 11, 2024

Content Search with Optimizely Graph

Optimizely Graph lets you fetch content and sync data from other Optimizely products. For content search, this lets you create custom search tools...

Dileep D | Oct 9, 2024 | Syndicated blog

Omnichannel Analytics Simplified – Optimizely Acquires Netspring

Recently, the news broke that Optimizely acquired Netspring, a warehouse-native analytics platform. I’ll admit, I hadn’t heard of Netspring before,...

Alex Harris - Perficient | Oct 9, 2024 | Syndicated blog

Problem with language file localization after upgrading to Optimizely CMS 12

Avoid common problems with xml file localization when upgrading from Optimizely CMS 11 to CMS 12.

Tomas Hensrud Gulla | Oct 9, 2024 | Syndicated blog