EPiServer  /  CMS August 02, 2009

Define tabs in code with Page Type Builder 0.8.5

The latest version of Page Type Builder for EPiServer CMS which was just released features a new way of working with tabs plus a few minor fixes and changes.

A new way to work with tabs

In previous versions of PTB you would define which tab a property would reside in by specifying it’s name, like this:

[PageTypeProperty(Tab = "NameOfTab")]
public virtual string MainBody { get; set; }

If no tab name was specified the property would be put on the first tab, usually the one named Information. This is still the case but the way to explicitly specify a tab for a property has now changed from specifying a tab name to specifying a type. The type must inherit from an abstract class in PTB called Tab. It must also be concrete. Both of these requirements are validated at start up.

An example of the new way to specify a tab for a property:

[PageTypeProperty(Tab = typeof(MyTab)]
public virtual string MainBody { get; set; }

Creating tabs

When you inherit from Tab you must implement three properties, Name, RequiredAccessLevel and SortOrder. By doing so you gain the same control over a tab as you get in EPiServers admin mode. An example tab can look like this:

namespace MySite.PageTypes.Tabs
{
    public class MyTab : Tab
    {
        public override string Name
        {
            get { return "MyTab"; }
        }

        public override AccessLevel RequiredAccess
        {
            get { return AccessLevel.Read; }
        }

        public override int SortIndex
        {
            get { return 11; }
        }
    }
}

Note that the key used to identify tabs is the name. This means that you can define the standard tabs, such as Information, Advanced and Categories in code by creating tab classes with the same name. One caveat of this, which follows the same pattern as for page types (unless you specifiy a GUID) and page type properties, is that you can’t rename a tab from code. To do that you should first rename it in admin mode and then in the code.

Other changes

This release also features a fix for a bug in version 0.8 which caused the startup validation of page types to fail if you had a automatic property that wasn’t virtual, even though it didn’t have a PageTypeProperty attribute, in a page type class.

I’ve also renamed the GetTypeForPageType method in PageTypeResolver to GetType and added a new method to the same class, GetPageTypeID. GetPageTypeID takes a type as parameter and returns a nullable int with either the corresponding page type ID as value or null. While nothing in PTB requires this method I found myself needing it recently when I was creating a custom page provider.

Feedback

As usual I greatly value any and all feedback. This time, in preparation for version 0.9, I would especially like feedback about what exceptions those of you who are already using PTB are getting at startup so that I can put in validation for those things and make PTB throw exceptions with more explaining messages.

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.

Joel Abrahamsson

Joel Abrahamsson

I'm a passionate web developer and systems architect living in Stockholm, Sweden. I work as CTO for a large media site and enjoy developing with all technologies, especially .NET, Node.js, and ElasticSearch. Read more

Comments

comments powered by Disqus

My book

Want a structured way to learn EPiServer 7 development? Check out my book on Leanpub!

More about EPiServer CMS