EPiServer: Adding an XForm to a Page

by matt 20. November 2009 10:08

I wanted to have a little play with XForms last night and have to say I was a little surprised.  Not necessarily with the complexity of displaying a form on a page, but with the lack of documentation about how to do it.  Now that I've figure out a method of displaying an XForm, I though I would share it in the home the less people suffer the problem in the future.

First off, EPiServer has a page about developing with XForms which is worth a read, but it is by no means an exhaustive list of how to actually use an XForm.

To get a form on your page, do the following (well, this is what I did anyway):

  1. Create a new page template that is going to contain an XForm.  I called my XForm property customerForm.
  2. In the ASPX page, add an XFormsControl to the page where you want your form to display as follows:
    <XForms:XFormControl ID="customerForm" runat="server" EnableClientScript="true" />
  3. In your code behind, add the following to the Page_Init (cForm is a private variable of type EPiServer.XForms.XForm):
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!CurrentPage.Property["customerForm"].IsNull)
        {
            cForm = EPiServer.XForms.XForm.CreateInstance(new Guid((String)CurrentPage["customerForm"]));
            cForm.PageGuid = CurrentPage.PageGuid;
    
            if (Page.User.Identity.IsAuthenticated) cForm.PostingUser = Page.User.Identity.Name;
    
            customerForm.FormDefinition = cForm;
        }
    }

Now let me explain what this is doing.

The very first this we do is check whether or not our form property has been given a value when the current version of the page was created.  If it has not been set, then clearly we don’t want to do anything.  Assuming that a form has been specified, then we need to create an instance of that form.  We do this by using the Guid of the XForm property.  I’m also setting that page ID, which I just assumed was so that we can relate the saved information back to the relevant page in EPiServer – but you might not need to do that.

In addition I store the current users name (if authenticated) against the form definition.

Finally, we set the FormDefinition property of the XFormsControl to our newly created XForm instance.

If there is a better method, I look forward to hearing about it.  I think I may just create a server control to do this form me so that I can just drop a control on the page and specify the EPiServer property name. :)

Tags:

EPiServer

Comments

11/20/2009 10:32:57 AM #

If you're not looking to do anything fancy with the form, you could use:
<EPiServerTongroperty runat="server" PropertyName="Xform" />

There are also some code samples on EPiServer World dealing with xforms and events: world.episerver.com/.../ (bottom of page)

Then of course, there is a sample user control in the public template package.

Cheers

Greger Olofsson |

11/20/2009 11:50:20 AM #

Hmm - so now I feel a bit sheepish.  I had tried that before, but it didn't work.  Maybe it's bacause I was in CMS 4.6 or something - who knows.  Thanks for the tip, Greger

Matt |

11/21/2009 12:17:12 PM #

Fail....

Steve |

11/23/2009 9:29:17 AM #

I think 'Fail...' is a smidge harsh and somewhat unconstructive given that I had tried the property already and it had not worked.  Anyhow, the long-winded method can also be useful if you want to start overriding the default behaviour of the forms.

Matt |

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar