Certified EPiServer developer, architect and trainer

Main menu

Hibernating Rhinos has two really good products for profiling applications that are using NHibernate or Linq to SQL, NHibernate Profiler and Linq to Sql Profiler. To profile your application you need to reference an appender and initialize it upon startup. This can easily be done in several ways in EPiServer CMS, using HTTP modules, the application start event on HttpApplication or with PlugInAttributes to mention a few. I prefer to create an InitializationModule in a separate assembly, which gives you the ability to add and remove profiling by adding/removing the assembly to/form the Bin folder. This way, you don’t have to release a new version of your code to enable profiling and it’s easy to reuse in all your solutions.

In my previous post I described how to query an IIS log as if it was a database table using SQL Servers OPENROWSET. This post is a short follow up on that describing how to query EPiServer (log4net) logs in the same way. In EPiServers logfiles, there are no character sequence that can be used as row delimiter, which means we need to preprocess the file in some way and inject a good row delimiter. I’ve been using RxFind to inject <logdelim/> after each log entry in the file.

IIS logs are a good source of information when you want to analyze the behavior of your site. There are many tools and log parsers around, but I tend to prefer SQL Servers OPENROWSET to parse and query log files.

To use OPENROWSET you need to map the format of the file to a virtual database table using an Xml Format file. I use a file like this one:

EPiServer CMS 6 introduced new tools for site and license administration. One not so known feature in these tools are the ability to generate demo licenses. In admin mode, click Site Information under the Config tab. Select the License Information tab and scroll down to Download License section. Select Demo license and hit Download License. The license will be generated, downloaded and installed on your site automatically.

In my previous post I described how the number of clicks within edit mode can be reduced by adding keyboard shortcuts for common tasks like switching between tabs and saving pages. Another very common and click intensive task in EPiServer CMS is to set the value of PageReference properties. First the editor has to click the browse button on the edit control to bring up the Select Page dialog. Then the editor needs to navigate to the right page, select it and click the Select button. All these tasks involves clicking and is impossible (or at least hard) to do using only keyboard shortcuts. A better concept for setting page references would be to enable drag and drop from the edit tree to the right property control in edit panel.

When I started to look in to this I realized that enabling drag and drop from the standard page structure would be hard, because EPiServer CMS 6 uses drag and drop for reordering pages. I’m not saying it’s not doable, but it involves some tricky parts. So I started out creating a custom edit tree plug-in that lists some pages. In my example code, only the children of the start page are listed, but it’s quite easy to customize this plug-in if you need to.

First I tried to write all the JavaScript on my own, but it turned out to be really hard to support all the common browsers. I asked Ted Nyberg at Ted & Gustaf for some advice and he hinted me to look into the native drag and drop support in HTML5, which lead me to a great article by Zoltan Hawryluk on Cross browser HTML5 drag and drop. This article provides some helper classes to enable drag and drop which I use.

The implementation is quite easy. I have implemented a ControlAdapter where I add a CSS class to the InputPageReference property and register some JavaScript includes. The same JavaScript includes are added to the page tree plug-in (Pages.ascx). In InputPageReference.js I register events for dragstart, dragover, dragleave and drop on all textboxes and all InputPageReference controls. On the drop event I check if the data being dragged is of type pagereference and if the drop target is of type InputPageReference. If both are true, the control is updated with the correct values, otherwise I just fall back to the default browser behavior. The fallback is important to support drag and drop from the file manager.

One thing editors sometimes ask me is if it’s possible to reduce the number of mouse clicks within the EPiServer edit mode. We all know that creating even the simplest page requires a lot of clicks to get the page created and saved. One way to solve this could be the use of keyboard shortcuts. And it turns out to be quite easy to implement. I just uploaded some example code to the Code section (found here). The example contains of a plug-in and two JavaScript files. The plug-in will register the two JavaScript files each time the edit panel is loaded. Shortcuts.js is an open source library to register, listen and act on keyboard shortcuts. Editpanel.js is a jQuery script where we can bind up our shortcuts. In my example file, I have added shortcuts for the tabs in edit panel and to the Save, Save and view and Save and publish buttons. Feel free to add your own shortcuts.

Nowadays almost all EPiServer projects involves some kind of interactivity powered by jQuery and AJAX. When it come to frameworks, there are a lot of alternatives to choose from on both the client- and serverside. On the clientside, jQuery has emerged to be the alternative to use. On the serverside, Microsoft offers a couple of different alternatives and at a first glance one might think WCF is the way go. Personally I think WCF might be great for big distributed solutions but could be a bit to complex for just feeding jQuery with some simple data. Another strategy I’ve seen quite often is concatenation of strings to create simple JSON objects. Simple, but maybe not that good.

The most recent versions of EPiServer uses Microsoft .NET Framework 3.5 which includes some great stuff for writing services for scripting.

In this post I will add a simple service to the EPiServer Public Templates which will contain two methods, one for fetching a list of objects and one that accept an object as parameter and returns a string.

First we need to create a model object that we can serialize to JSON and send to the browser. Try to keep the model object as small as possible i.e. do not return the entire PageData object. Lets create a Person class.

Next, we need to add some jQuery code to consume our service. Add main.js to our project and make sure you include it in the header tag together with jQuery itself. Add the classic jQuery stub to main.js:

In my current project I need to know when the latest change was made on any page on the entire site. My first thought was to use FindPagesWithCriteria or maybe to do a recursive GetChildren to find the latest value of the PageData.Changed property. But since EPiServer CMS 6, there is a change log you can ask for this kind of stuff. There is even a public class, used by the Recently Changed Pages gadget you can use, RecentlyChangedPagesFinder.

Today I installed Page Type Builder 1.2 Beta 2 on my ongoing EPiServer CMS 6 project. I dropped the Page Type Builder assemblies in the bin folder, recreated the references, did an iisreset and then tried to start the site. I got the following exception:

The reason for this exception is Windows 7 security features. When you download assemblies from Internet they might be marked as unsafe. To solve this, right click each assembly, select Properties and click the Unblock button on the General tab.

Page Type Builder might be one of the best things that happened to EPiServer CMS over the last couple of years. I strongly recommend you to use it. With that said, I want to share a small debugging tip that is very useful while using Page Type Builder. Debugging is in general a lot about inspecting your objects values. Visual Studio offers different approaches for this, like watches, the immediate window or just hovering your objects and inspect the tooltip to mention a few. I usually prefer the last one. It is quick and easy but you often ends up in a lot of scrolling and traversing through deep object graphs. This tip is as simple as showing you how to customize the tooltip to display the most relevant values for you immediately.

Back to Page Type Builder, if you hover a page type class, all you get is the name of the anonymous type generated behind the scenes. Not so useful, which means you need to expand and scroll in the tooltip to find the PageLink for instance.

When working with Page Type Builder it’s recommended that you create a base class that all your page type classes inherit from. This class can be decorated with attributes which applies to all your page type classes. In this example I will use the DebuggerDisplayAttribute (see Using DebuggerDisplay Attribute for more info).