Deploying Visual Studio templates

Sharing Visual Studio templates with colleagues can be time consuming depending on the chosen distribution mechanism. This article shows how to package and deploy templates to a shared location, greatly simplifying the process of company wide template maintenance. Examples are based on Visual Studio 2010 and Windows 7.

Reason

To make use of Visual Studio item and project templates they need to be placed in an appropriate subfolder of the Visual Studio template directory, located in “[personal folder]\Documents\Visual Studio 2010\Templates” by default (e.g. “C:\Users\Uli\Documents\Visual Studio 2010\Templates”). Changing this location to a central storage must be done on each computer in question and consists of changing two registry entries.

The default set of project and item templates that are installed with Visual Studio are stored as ZIP-archives. Custom templates can be stored as ZIP-archives as well, but don’t have to — they can also simply be stored as unpacked files in an appropriate subfolder of the Visual Studio template directory (e.g. “[…]\Visual Studio 2010\Templates\ProjectTemplates\Visual C#\MyCompany\SitecoreProjectTemplate\”). Personally I find it easier to maintain non-zipped templates, but YMMV.

Code

Template packaging

Creating a zipped template package consists of adding all relevant files to an archive following these rules:

  • The archive must contain a template definition (.vstemplate-file).
  • The template definition must be located directly in the archive root. Both images below show the root of a ZIP-archive intended to be used as a Visual Studio template. In the first image the .vstemplate-file has incorrectly been placed within a subfolder of the archive.
Incorrect package

Incorrect package

Correct package

Correct package

  • All paths to files and folders listed in the template manifest (vstemplate file) must be placed relative to it in the archive root.

The contents of an item template are discussed in Creating Visual Studio item templates, project templates are discussed in Creating Visual Studio project templates.

Template distribution

By default, templates are placed in a folder hierarchy as outlined below. “CompanyName” indicates a manually created subfolder where custom templates are stored.

  • TemplateRoot
    • ItemTemplates
      • Visual Basic
      • Visual C#
        • CompanyName
      • Visual Web Developer
    • ProjectTemplates
      • Visual Basic
      • Visual C#
        • CompanyName
      • Visual Web Developer

Changing the default template location is done by opening “Tools → Options” withing Visual Studio and changing the settings shown below.

Changing user template locations

Rather than asking every developer in a company to change their settings manually, a script can be created which changes the registry entries containing the folder paths of interest — the benefit being that misspelling and other human errors are minimized, and that a script can be applied across an entire company by e.g. a system administrator.

The template folder settings are located in “HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0”.

Visual Studio item and templae folder registry values

Shown below is the batch file version of such a script. Although it’s possible to do the same using a .reg-file, due to the hexadecimal notation used in REG_EXPAND_SZ-entries batch files are easier to read and modify.

REG ADD "HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0" /v "UserItemTemplatesLocation" /t REG_EXPAND_SZ /d "\\Company\Network\Drive\ItemTemplates" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0" /v "UserProjectTemplatesLocation" /t REG_EXPAND_SZ /d "\\Company\Network\Drive\ProjectTemplates" /f

Example

Shown below is an example of a deployed item template, and the corresponding insert option in the “Add New Item”-dialog within Visual Studio. As can be seen, the folder in which the template is placed servers as a “subcategory” in the left hand menu, while the template name, description and similar values from the template definition are displayed in their respective locations.

Visual Studio template folder structure

Visual Studio "Add new item"-dialog

A batch file which changes the registry must be run using administrative privileges. An example of such a run is shown below.