Code Snippets to PageTypeBuilder

UPDATE: These snippets are out of date, download from this post instead

For you who have developed with the open source CMS N2 CMS, I guess you fell in love in how you are in control over the page type properties. And as a few of you have might notice the newly announced EMVP Joel Abrahamsson has found a really nice way to achieve this with EPiServer.

To create a property there are a few lines of code, and sometimes you might be to lazy to write these lines.. And as you may know there is a great thing in Visual Studio called Snippets that can help you out. A snippet is a XML-based file, and if you are lazy as I am, you probably want some help when you create your own snippets. I can recommend using a program called Snippy.

Here is a step by step how to create your own snippet. In this example I will create a snippet for a “Short string property” to the PageTypeBuilder earlier mentioned. (if you just want to download the snippets, check at the bottom of this post)

  1. Download and start Snippy
  2. Start by entering some information about the snippet.
    snippet_1
    The shortcut field is what you have to write in Visual studion to use the snippet.
  3. In the Code-area, change language to csharp and paste the code that you would like to make a snippet of. In my case I will use the code to create a definition for a Short string.
    [PageTypeProperty(SortOrder = 100, UniqueValuePerLanguage = true, Searchable=true, Type = typeof(PropertyString))]
    public string Headline
    {
    get
    {
    return GetPropertyValue<StartPageType, string>(page => page.Headline);
    }
    
    set
    {
    SetPropertyValue<StartPageType, string>(page => page.Headline, value);
    }
    }
    

    snippet_2

  4. If we save the snippet now, we would actually have a functional snippet, but there a few things that we would like to change..
    In this snippet I would like to change SortOrder, UniqueValuePerLangugae, Searchable, Headline(PropertyName) and the StartPageType(which pagetype this property should be saved/read to/from)
    In “Literal & Objects” we can define so these things mentioned above could be changed by just tabbing around when we are using our snippet.
  5. Let’s start with SortOrder. Press the “Add…”-button.
    • ID: the “variable”-name that we will refer to in the code-area
    • Tooltip: a little help-text when you are using the snippet
    • Default value: when using the snippet and you ignore this part you get the default value
    • Function: There are a few predefined functions you can use, we will use one later to retrive the class name of the page type. You can read more about the functions here: http://msdn.microsoft.com/en-us/library/ms242312(VS.80).aspx
    • Declaration type: Yo can read about the difference here: http://msdn.microsoft.com/en-us/library/ms165396(VS.80).aspx. We will use Literals.
    • Editable: Everything except the PageType should be editable by the user

    This is how the SortOrder should look like.
    snippet_4

    And when we refer to it in the code area we use the ID surronded with $-signs, like $SortOrder$.
    snippet_5

  6. Continue with the others, the only special one is the PageType. For this one we need to use a function called ClassName() and the Editable-box should not be checked.
    snippet_6
  7. This is how it should look like when you are done:
    snippet_7

    snippet_8
    As yo can see the PropertyName is used several times, but you will only need to enter it once.

  8. Now save your snippet. I think the right directory will choosen, if not it should be saved in “My Documents\Visual Studio [2005/2008]\Code Snippets\Visual C#\My Code Snippets”
  9. Open up visual studio, and in a class start typing the shorcut name given, and it will be shown in the intellisene.
    snippet_9
  10. Press Tab, Tab and you can now use your snippet.
    snippet_10

I’ve only created three snippets so far. You can download them here! (Unzip in “My Documents\Visual Studio [2005/2008]\Code Snippets\Visual C#\My Code Snippets”)

Feel free to contribute with your own snippets to the PageTypeBuilder, I will probably update this blog post as a create new ones.

My Documents\Visual Studio [2005/2008]\Code Snippets\Visual C#\My Code Snippets