Using XForms with Page Type Builder

A former colleague of mine, Cecilia, just asked me how to access information about a XForm when using Page Type Builder. Normally when you use Page Type Builder you access the value of (EPiServer) properties with code properties such as this:

[PageTypeProperty]
public virtual string MyProperty { get; set; }

//Or..

[PageTypeProperty]
public virtual string MyProperty
{
    get
    {
        return this.GetPropertyValue(page => page.MyProperty);
    }
}

But when it comes to a XForm you probably won’t be interested in the property’s value but instead in the property it self, or perhaps as in Cecilia’s case, in the property’s Form property from which you can get information about the form such as the form’s name. To make your code property return the actual property you can implement it like this:

[PageTypeProperty(Type = typeof(PropertyXForm)]
public virtual PropertyXForm MyFormProperty
{ 
    get
    {
        return  (PropertyXForm) 
            this.Property[this.GetPropertyName(
                page => page.XFormTeaser)];
    } 
}

To instead make the code property return the actual XForm object you can implement it like this:

[PageTypeProperty(Type = typeof(PropertyXForm), Tab = typeof(Poll), SortOrder = 80, EditCaption = "Veckans fråga")]
public virtual XForm MyFormProperty
{ 
    get
    {
        return  ((PropertyXForm) 
            this.Property[this.GetPropertyName(
                page => page.XFormTeaser)]).Form;
    } 
}

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Follow me on Twitter

  1. @chraas You might want to use it for complex parts of the site and not use it for simple rendering pages. 1 days ago
  2. @chraas In theory more SOLID, but I'm not sure it's worth the price. 1 days ago
  3. @chraas Be warned that you're introducing one big chunk of complexity with EPiMVP :) 1 days ago
follow me

Latest comments

  1. Berra S wrote "Read your post at http://joelabrahamsson.com/entry/using-xfo..." on PageData objects not returned as typed when using Page Type Builder and FindPagesWithCriteria
  2. Linus wrote "1 up for behaviour being as close as expected as possible!" on A common problem with Page Type Builder and UniqueValuePerLanguage set to false
  3. Joel Abrahamsson wrote "Hi Hans, Could it be that you previously didn't have Page..." on Page Type Builder 2.0 released

About this site

This blog is built with EPiServer Community, EPiServer CMS, ASP.NET MVC and a bunch of other great products. The source code is available for download at the projects page, where you also can read more about this site and my other projects.

read more