Using PageTypeBuilder with Composer
A colleague of mine had trouble using PageTypeBuilder on a project that was also using composer. Since going back to the dark ages without PTB seemed like some mild fun I decided to look into the issue.
Finding the error
To reproduce this bug I installed a site with CMS 5 R2 SP2 and Composer 3.2.6 and created a test page of type Composer Wide Page. I then added the PageTypeBuilder dlls to the project and when you do a Composer – edit on page (from the right click menu) on the created test page you get the following exception:
System.NullReferenceException: Object reference not set to an instance of an object.
Don’t you just love that exception?
The stack trace looks like this (I’ve removed the uninteresting parts):
We did some quick googling and found this blog post from Tomas Unestad at DropIt but that didn’t seem to be the problem we were getting here. We also found this forum post that seemed to be getting exactly the same error as we were experiencing but it didn’t have any replies.
So I fired up reflector and poked around the LanguageBranchControl to see what was going on and what was triggering PTB. It didn’t take long to found these lines
GetPage will invoke PTB code because it hooks up to the event of a page being loaded (to be able to cast the page to the typed version). I noticed here that after the call to GetPage a check is performed to make sure that the returned page was not null. And PTB is throwing a NullReferenceException. That can’t be a coincidence.
“Fixing” the error
To be able to look at what’s happening I downloaded the source code to PTB and added it to my solution and instructed my EPi-project to work with that instead of the precompiled dll. I did a simple addition to the method that handles the event of a page being loaded from DataFactory.
With the added lines 3-5 I also added a breakpoint there and tried the edit on page again. The breakpoint indeed was hit and I got the following info from the PageEventArgs:
PageLink 29 is my created test page (of type Composer Wide Type). Why this is sent here with a null Page I have no idea. The good news is that the application with the null check in place no longer throws an exception and works as expected.
So, you know… what about actually using PTB with composer functions?
Ehm yea, about that. I started looking into that but it turned out to be slightly more complex than I first had thought (at least with my non existing knowledge of Composer). Not impossible but a bit too much work for a casual Saturday post.
Maybe in a future post.