PageTypeBuilder and EPiServer CMS 6 R2 Synching Issues – Important
Background
There have been a couple of issues that have been logged on codeplex to do with long start up times and default values not being set on page definition properties, the links to the issues are below:
- After upgrading to PTB 2.0, starting site takes a really long time
Basically there was a change in CMS 6 R2 which changed how sort orders are handled. When PageTypeBuilder is synching it will compare whether the page definition is different to the one stored in the database and if so it will update it.
In CMS 6 R2 a new method is now called named EnsureUniqueFieldOrder within the Save method of the PageDefinition.
At a high level this method does the following:
- Checks whether the sort order for the current page definition matches the sort order of any other page definitions for the page type and if it does it will set a new value for it.
- If a sort order has not been defined and there are existing page definitions it will get the highest sort order and add 1 to it.
This then has a potentially huge knock on effect because the next time the application starts PageTypeBuilder will go off and work it’s magic, but because the EnsureUniqueFieldOrder method has changed some of the sort orders, PageTypeBuilder will determine that the page definitions are different and update the definitions again .
So to put simply this is not a bug in PageTypeBuilder or EPiServer it’s just a unfortunate side effect of not defining your Sort Order's in your page types or specifying duplicates. Over time we often have many common base classes that page types will inherit from and can often be a case for many duplicates.
Introducing PageTypeBuilder.SortOrderChecker
Some of you may be aware that I spend a lot of time commuting to and from London town and that I like to fill this time with knocking together useful modules and applications.
So last week to fill this time I decided to knock together a little console application which will scan a bin folder and tell you which page types have duplicate and undefined properties.
It can also fix all of the page type files for you (assuming you are programming in C#) and apply new sort orders for various property definitions including property groups. The fixing code will also fix sorting on page type and property group base classes too to ensure there are no duplicates.
Using PageTypeBuilder.SortOrderChecker
Firstly you will need to Download the executable and it’s config file from here and dump it in a folder on your machine. Then you need to follow the steps below:
2. Backup any relevant EPiServer databases and source files
3. Then you can execute the tool with the following arguments to determine which properties have duplicate or undefined sort orders.
c:\Tools\PageTypeBuilder.SortOrderChecker.exe -t:0 -b:c:\episerver\sites\pagetypebuildertest\bin
You will be shown a window like the following:
4. You can then attempt to fix your page types by running the following command, please note you may need to close visual studio first just in case there are any file locks.
c:\Tools\PageTypeBuilder.SortOrderChecker.exe -t:2 -b:c:\episerver\sites\pagetypebuildertest\bin -p:c:\episerver\sites\pagetypebuildertest
You will be shown a window like the following:
5. Now that your files have been fixed, you will need to rebuild the solution. It may be possible that some classes/properties will not be updated, but this would probably be because of bad coding style, not by me of course
Now we have rebuilt we can run the tool again to check for duplicates and unassigned sort orders.
c:\Tools\PageTypeBuilder.SortOrderChecker.exe -t:0 -b:c:\episerver\sites\pagetypebuildertest\bin
This time you will hopefully be told that all duplicates and unassigned sort orders are no more .
6. One thing to be aware of is that as EPiServer has done it’s own thing for duplicate and undefined sort orders, it may still be likely that are some duplicates even though there are none in code.
To get round this you can run the tool again with the following commands.
c:\Tools\PageTypeBuilder.SortOrderChecker.exe -t:3 -b:c:\episerver\sites\pagetypebuildertest\bin -o:c:\temp\ResetSortOrders.sql
This will create a .sql file with the necessary SQL to reset all PageTypeBuilder Property Definition sort orders to 0, so you will need to execute the SQL against your database.
If you have a large number of page types and page definitions this initial update may take a bit longer than expected, but once this initial update is complete all future deployments/application restarts should be much quicker .
Checking Sort Orders on post build event
You can hook the sort order checking executable into your application build by calling it on a post build event, it's not particularly elegant but will fail the build if there are duplicate or undefined sort orders and will output duplications and undefined properties on page types to the output window. Command for post build:
c:\Tools\PageTypeBuilder.SortOrderChecker.exe -t:1 -b:$(TargetDir)
Disclaimer
Although I have done testing around this there could be possible side effects. I take no responsibility for any adverse affects. So basically use at your own risk and backup your database and source code first.
Feedback
Please feel free to email or twitter me with any feedback @croweman
Nice, very nice.
If anyone is brave enough it would be interesting to find the maximum number of duplicate/undefined sort orders and hear about the performance increase differences :)
Nice :) Our project implemented a similar, but more basic console app two weeks ago. Lots of pagetypes and several properties with duplicate sortorder. We reduced startuptime from 18 minutes to 2 minutes :-D
Great tool! Reduced start up time at about 30%.
P.S. Also mentioned a little bug, that if pagetype property is empty like PageType (), it adds SortOrder with comma like PageType (SortOrder = 32, ), that leads to an error when compiling.
Thanks)
Cool, I did consider the empty PageType attribute but I didn't think anyone would actually do that ;)
Great one. Managed to knock off about 40% off the start up time.
Now just got to get the other devs not to go and mess up my lovely ordering.....
Hi James
If you add the post build event mentioned in the article to the build the over dev's will not be able to break it :)
Lee
To verify that there are no more duplicates in the database you can you the following query, it should return no results. The where condition ignores undefined sort orders.
SELECT [fkPageTypeID], [FieldOrder]
FROM [tblPageDefinition]
WHERE [FieldOrder] > 0
GROUP BY [fkPagetypeID], [FieldOrder]
HAVING COUNT([FieldOrder]) > 1
God bless you!!
Thanks for re-tweeting this today Lee, I'd totally forgotten about it, and have been suffering with slow startups on the project I'm working on this week.
I'd used the PTB classes to discover properties which were using the default sort order, or were out of sync with the DB. There were loads, so I opted not to manually update them all at that point......thankfully.
I've just run your console app, and there were:
131 properties have duplicate sort orders
1408 properties have undefined sort orders
1539 properties have duplicate or undefined sort orders
page load took 144.109 seconds.
Running the fix reduced page load to 29.544 seconds and saved me from manually updating all the code!
I owe you a beer :)
Thanks a lot croweman this helped me...
Cheers :-)