Linus Ekström
Oct 30, 2008
  4299
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024