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):
- Create a new page template that is going to contain an XForm. I called my XForm property customerForm.
- 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" />
- 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. :)
c1d7b71b-5942-46b2-aa82-418ad2dcc591|0|.0
Tags:
EPiServer