Creating EPiServer Page Types using ReSharper File Templates

When creating new page types, developers mostly use the EPiServer CMS Visual Studio Extension or copy and paste the code from existing files. In this blog post, I’ll show you how to create EPiServer page types using ReSharper file templates. Once you learn all the shortcuts, it becomes much faster to generate code using ReSharper, rather than copy and pasting.

Creating a Template

1. In Visual Studio, choose ReSharper > Tools > Templates Explorer.

2. In Templates Explorer, select File Templates > C#, and then click the New Template button.

 

3. Change the default file name and description, and write the following code:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.Web;

namespace $NAMESPACE$
{
    [ContentType(GUID = "$GUID$")]
    public class $CLASS_NAME$ : PageData {$END$}
}

 We have now defined the following three parameters: $NAMESPACE$, $GUID$ and $CLASS_NAME$.

4. To replace $NAMESPACE$ with a proper namespace (when creating a new file), we need to uncheck Editable and choose the Default namespace for current file macro.

5. We need to do the same for the $GUID$ and $CLASS_NAME$ parameters.

For the $GUID$ parameter, choose the New GUID macro.

For the $CLASS_NAME$ parameter, choose Current file name without extension.

Creating a New File From the Template

Now we can create a new class using our template.

1. In Solution Explorer, select a folder where you want to create a new file, and then press Ctrl+Alt+Insert, or from the main menu, choose ReSharper > Edit > Create New File.

2. Click More… and select EPiServer PageData from the list of available templates.

Tip: Check Add to quicklist to have EPiServer PagaData listed in the Create File From Template context menu. Next time you can skip the Choose Template window by pressing Ctrl+Alt+Insert and D.

3. Enter the file name, and then click ok.

Pro Tip: You don’t have to include the “.cs” file extension.

ReSharper will then generate the following code:

You’ll notice that we have some unused namespaces, such as EPiServer.DataAbstractions, EPiServer.Web, etc. That’s because our MyPage page type will probably have some properties that will be decorated with Display and UIHint attributes, which require those namespaces.

You can save a few keystrokes by including extra namespaces and running the code cleanup when you’re done. Having the Import ‘…’ and other missing references? message popping up all the time can be quite annoying.

Conclusion

When creating ReSharper templates for real-world projects, you’ll probably have to use some other project-specific base classes instead of PageData. And once you learn all the keyboard shortcuts, ReSharper will be your #1 productivity tool for EPiServer development.

comments powered by Disqus