Monday 4 February 2008

Extend The PublishingLayoutPage Class - Create Your Own to Run Code in the 'Page'

This is powerful stuff, and thanks to this I can do a lot more powerful MOSS WCM custom development.
This will let you run your own custom code in the context of a page.
Do you miss putting code in the Page? I do!
This will kinda give you back some of that 'lost power'.
http://blogs.msdn.com/jannemattila/archive/2007/04/14/adding-functionalities-to-pages-by-inheriting-publishingpagelayout.aspx (awesome post that started me on this!)
microsoft.sharepoint.publishing.publishinglayoutpagehttp://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishinglayoutpage_members.aspx

You may notice that all MOSS WCM Page Layouts start with the following Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c"
So if you create your own custom class, which inherits from PublishingLayoutPage you can then define your own methods and properties in it, do page level code like we used to do in MCMS !
You then switch the reference in you Page Layouts to use your new custom PublishingLayoutPage as described in Janne Mattila's EXCELLENT post.
I have tried this out and it works like a charm, I can now run code code when the page initialises, such as switch the Master Page used on loading the page, awesome.

Many Custom PublishingLayoutPage Classes, One for Each Page Layout ?
If you develop your own custom PublishingLayoutPage class then you can reference it in All your custom Page Layouts. The same custom code will now run in All Page Layouts.
To go one step further, develop many custom PublishingLayoutPage classes, one for each page layout you have, you can then run independent code for each Page Layout!
Even create your own Base PublishingLayoutPage class and inherit many custom classes from your base, this will give you the ability to run independent code for each page layout, AND also run common code for all Page Layouts in your Base PublishingLayoutPage class.
The possibilities are endless. My only regret is not trying this out sooner =o(

No comments: