Blog

IntelliSense for document properties in Visual Studio when not using Models Builder

Although the new Models Builder that was introduced with Umbraco 7.4 (well, actually it had already been a plugin for some time before that) is extremely useful, there are times when you don't want to (or can't) use those new strongly-typed models for your Umbraco templates, and you have to resort to specifying property names using hard-coded strings.

Using strings increases chances for typos, typos lead to errors, errors lead to debugging, debugging leads to more overall development time, and this leads to you not meeting deadlines, and/or getting paid less related to the time you spent developing / debugging (if you're a freelancer, for example). 

We've had this problem for some time, and although one can get away with it when developing simple projects it often became an overwhelming issue when we had to deal with complex Umbraco setups with hundreds of properties scattered across dozens of document types (which is usually the norm).

So my partner, George, did what he always does best - solved the problem. He put together a nice T4 template which generates a set of classes under the namespace DotSee.PropSense with constants that correspond to document property aliases.

Here's an example of such a generated class for the document type alias "dContentSliderItem" (yes, we sometimes actually *use* such names, some hate it, some love it):

public static class Psn_dContentSliderItem
{
public const string TypeName = "dContentSliderItem";
public const string internalLink = "internalLink";
public const string externalLink = "externalLink";
public const string content = "content";
public const string buttonText = "buttonText";
public const string image = "image";
public const string imagePosition = "imagePosition";
}

All classes get the prefix "Psn_" for easy IntelliSense and their properties correspond to document property aliases (compositions included). So, next time you need to do something like:

someNode.GetPropertyValue("internalLink")

you can instead do:

someNode.GetPropertyValue(Psn_dContentSliderItem.internalLink)

Of course this will come out longer and with less respect for conventions (if the alias starts with a lowercase letter, then the property will also start with lowercase) - But it gives you IntelliSense, so you will be free from potential typos.

As you can see, there's also the TypeName property which gives you the document type alias in case you need it somewhere.

How to install the PropSense T4 Template:

  • Get the package from Our Umbraco (or get the template from GitHub and just put the files in your App_Code folder) - see links below
  • In Visual Studio, right-click-"Run Custom Tool" on UmbracoData.tt. Your classes will be created and you'll be ready to use them in Visual Studio.
  • Just include the namespace DotSee.PropSense in your view and start typing "Psn_" and there's your IntelliSense. 

Please remember to re-run this whenever you make changes to document properties.

Known issues
This won't (yet) work with SQL Server CE connection strings.

 

Get the source code at GitHub

Download the package from Our Umbraco



;