Create Optimizely Model properties using code snippets

Published: 18 Sept 2018. Estimated read time about 3 minutes.
👨‍💻This post was written by a human.
Categories: (optimizely)
Create Optimizely Model properties using code snippets

As an Episerver developer you will be creating properties on your Page/Block/Media models a lot, and it’s generally a pain to type everything out again and again. Fortunately for us, Visual Studio supports the notion of a Code Snippet that we all know and love such as ctor, cw, and even prop.

While prop gets us a long way you still want to be setting up the [Display()], [CultureSpecific], and even [UIHint()] attributes to improve the editor experience and follow general best practice. In that vein, I’ve created a set of Episerver-specific code snippets to make life easier for someone as type-lazy as I am and I thought I should share it as it truly is a productivity improvement.

snippets in action

This is the full list:

// Snippet shortcut followed by result

//epstr
[CultureSpecific]
[Display(
    Name = "Heading",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 10)]
[UIHint(UIHint.LongString)]
public virtual string Heading { get; set; }

//eprte
[CultureSpecific]
[Display(
    Name = "Body",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 10)]
public virtual XhtmlString Body { get; set; }

//epint
[Display(
    Name = "Number",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 10)]
public virtual int Number { get; set; }

//epbool
[CultureSpecific]
[Display(
    Name = "DisplaySection",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 10)]
public virtual bool DisplaySection { get; set; }

//epcntr
[CultureSpecific]
[Display(
    Name = "SelectedContent",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 10)]
[AllowedTypes(new[] { typeof(Block) })]
public virtual ContentReference SelectedContent { get; set; }

//epcntri
[CultureSpecific]
[Display(
    Name = "SelectedContent",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 10)]
[UIHint(UIHint.Image)]
public virtual ContentReference SelectedContentImage { get; set; }

//epcnta
[CultureSpecific]
[Display(
    Name = "BlockArea",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 10)]
[AllowedTypes(new[] { typeof(Block) })]
public virtual ContentArea BlockArea { get; set; }

For simplicity’s sake, each file name corresponds to the shortcut for using that snippet, and all “Name” fields contain a single word for double-click-to-select convenience.

The list of snippets

First, you need to download and extract the snippets from here 👉 Epi_VS_Snippets.zip 👈

Then open up Visual Studio and go to Tools > Code Snippets Manager (or hit Ctrl+K, Ctrl + B)

Open the Code Snippet Manager

Select the Import.. button

Select Import

Navigate to where you extracted the zip file from above, select all snippets and click open

Select all the downloaded snippets

Make sure “My Code Snippets” is ticked as the location, and click Finish

Make sure to import to My Code Snippets

Finally, close out of the Code Snippets Manager by clicking OK.

Congratulations that’s all there is to it, now you can use these snippets and save yourself some effort by hitting tab twice.

Feel free to update these snippets as you see fit and then reimport them into VS, or if these inspire you to create more cool ones why not come share them with us on github.

Thanks for stopping by.

Recommended Posts

Back to top ⇈